Regular expressions: how to get an iterator for regex_match?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Regular expressions: how to get an iterator for regex_match?

  1. #1
    Join Date
    May 2014
    Posts
    2

    Question Regular expressions: how to get an iterator for regex_match?

    In order to parse mathematical expressions I am trying regular expressions and a recursive algorithm, but I have a problem with the four basic operations: +, -, *, /.

    Trying to analyze a string like "a+(b+c)", if I use the pattern for a sum "(.+)\\+(.+)" the program matches it recognizing as subpatterns: "a+(b" and "c". How could I achieve the program to try also the other possibility?

    I think that it would be great something like an regex_iterator which worked with regex_match instead of regex_search. I mean, an iterator that iterates over all the possible ways to match a given regular expression and a given string. This way I could loop through all these possibilities until the two subpatterns produced were correct mathematical expressions.

    I know, unfortunately there is no such thing. But since I have no experience with regular expressions, I wonder if there is other way to get such an elegant solution or another approach with the usual tools, maybe using a different pattern.

    I would appreciate any hint. Thank you!

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    Re: Regular expressions: how to get an iterator for regex_match?

    you might be able to get a bunch of code to work for mathematical expressions, but the language isn't regular, so it'll involve multiple regexes cascaded together somehow.

    Typically speaking, it's not the most efficient nor easiest way to make a math expression parser/evaluator.

    so unless you need this for a educational project (showing when and where regexes are and aren't appropriate) or satisfying a masochistic tendencies... try a different approach

  3. #3
    Join Date
    May 2014
    Posts
    2

    Re: Regular expressions: how to get an iterator for regex_match?

    Than you OReubens!. I am following your advice and trying another aproach. Regular expressions will help with some details but not with that task.
    It's amazing how a wise advice from a person with a broader view in a particular field can save us a lot of time! That's the grandeur of internet ...and people.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    Re: Regular expressions: how to get an iterator for regex_match?

    Well it's a common issue with regex...

    Regular expressions are used for describing "context-free languages with regular grammar"

    if your language grammar is irregular, or if it requires context to parse, then regex is probably not the best way to get the job done. You may be able to get a specific subset of the language to work with regex, but it'll be impossible to do it all with a single expression.

    The line has become a bit faded since the original regex, since we now have back references (which imply context) so "some" context is currently possible with regex.


    what you want is an "infix notation" expression parser.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center