Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. Parsing a string with match groups

Parsing a string with match groups

Scheduled Pinned Locked Moved Regular Expressions
regexdatabasejsonhelptutorial
3 Posts 3 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dominick Marciano
    wrote on last edited by
    #1

    Let me start by saying that I'm not the greatest with Regular Expressions, but from research it seems that they are the best way to go for what I need; I just don't fully understand how to use them with matching groups. In my application I need to parse a boolean string that can have any number of parameters. The following are some examples:

    Name=John or Name=Jane
    Name=John or Name=Jane and Date=Now

    I need to be parse any variation of these strings ('and' and 'or' are the only boolean operators that can be present) so that I get a list such as:

    Name=John
    or
    Name=Jane
    and
    Date=Now

    I don't need to parse the strings with the '=' sign, but I do need the 'or' & 'and' operators so when I build my query in code I know how to connect each statement to the next (ie:Name=John should be ORed with Name=Jane which should be ANDed with Date=Now) So far I have only been able to get a list such as

    Name=John
    Name=Jane
    Date=Now

    by using the following code:

    Dim Pattern As String = "(\S+\x3D\S+)"

    While Regex.Matches(query, Pattern).Count > 0
    Dim oMatches As MatchCollection = Regex.Matches(query, Pattern)
    For Each oMatch In oMatches
    Dim Operand1 = oMatch.Groups(0).Value
    Next
    End While

    but I lose the boolean operators in the process. If anyone could please help me with a regular expression so I would get the groups I have now, but with the operators in between the appropriate expressions, it would be greatly appreciated. Thanks.

    M G 2 Replies Last reply
    0
    • D Dominick Marciano

      Let me start by saying that I'm not the greatest with Regular Expressions, but from research it seems that they are the best way to go for what I need; I just don't fully understand how to use them with matching groups. In my application I need to parse a boolean string that can have any number of parameters. The following are some examples:

      Name=John or Name=Jane
      Name=John or Name=Jane and Date=Now

      I need to be parse any variation of these strings ('and' and 'or' are the only boolean operators that can be present) so that I get a list such as:

      Name=John
      or
      Name=Jane
      and
      Date=Now

      I don't need to parse the strings with the '=' sign, but I do need the 'or' & 'and' operators so when I build my query in code I know how to connect each statement to the next (ie:Name=John should be ORed with Name=Jane which should be ANDed with Date=Now) So far I have only been able to get a list such as

      Name=John
      Name=Jane
      Date=Now

      by using the following code:

      Dim Pattern As String = "(\S+\x3D\S+)"

      While Regex.Matches(query, Pattern).Count > 0
      Dim oMatches As MatchCollection = Regex.Matches(query, Pattern)
      For Each oMatch In oMatches
      Dim Operand1 = oMatch.Groups(0).Value
      Next
      End While

      but I lose the boolean operators in the process. If anyone could please help me with a regular expression so I would get the groups I have now, but with the operators in between the appropriate expressions, it would be greatly appreciated. Thanks.

      M Offline
      M Offline
      Matt T Heffron
      wrote on last edited by
      #2

      I'd suggest doing the matches in phases so that you can track operator presedence: and is higher presendence than or: In fact you probably don't need to use Regex, String.Split should be able to do the job: First divide the input based on 'and':

      Dim andSeparators() As String = {" and "}
      Dim andTerms() as String
      andTerms = String.Split(andSeparators, query)

      Next for each of the andTerms do a similar String.Split on " or "

      1 Reply Last reply
      0
      • D Dominick Marciano

        Let me start by saying that I'm not the greatest with Regular Expressions, but from research it seems that they are the best way to go for what I need; I just don't fully understand how to use them with matching groups. In my application I need to parse a boolean string that can have any number of parameters. The following are some examples:

        Name=John or Name=Jane
        Name=John or Name=Jane and Date=Now

        I need to be parse any variation of these strings ('and' and 'or' are the only boolean operators that can be present) so that I get a list such as:

        Name=John
        or
        Name=Jane
        and
        Date=Now

        I don't need to parse the strings with the '=' sign, but I do need the 'or' & 'and' operators so when I build my query in code I know how to connect each statement to the next (ie:Name=John should be ORed with Name=Jane which should be ANDed with Date=Now) So far I have only been able to get a list such as

        Name=John
        Name=Jane
        Date=Now

        by using the following code:

        Dim Pattern As String = "(\S+\x3D\S+)"

        While Regex.Matches(query, Pattern).Count > 0
        Dim oMatches As MatchCollection = Regex.Matches(query, Pattern)
        For Each oMatch In oMatches
        Dim Operand1 = oMatch.Groups(0).Value
        Next
        End While

        but I lose the boolean operators in the process. If anyone could please help me with a regular expression so I would get the groups I have now, but with the operators in between the appropriate expressions, it would be greatly appreciated. Thanks.

        G Offline
        G Offline
        Gabriel Szabo
        wrote on last edited by
        #3

        You can use the alternation syntax "(a|b)". A match in your case is an expression in form "parameter=value", or an operator. Operator can be "or" or "and" and must have a space before and after. Following pattern produces the results you wanted. For an input string "Name=John or Name=Jane and Date=Now" you get matches "Name=John", " or ", "Name=Jane", " and ", "Date=Now":

        "(\w+=\w+|\s(or|and)\s)"

        If you want to use regex for validation only, you can do it this way (note that you get only a single match with this pattern):

        Regex.IsMatch(inputStr, "^(\w+=\w+|\s(or|and)\s)+$");

        And you can go even further with validation. Following pattern uses positive look ahead/behind syntax to ensure that operators are enclosed with valid expressions:

        Regex.IsMatch(inputStr, "^(\w+=\w+|(?<=\w+=\w+)\s(or|and)\s(?=\w+=\w+))+$");

        Gabriel Szabo

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups