Parsing a string with match groups
-
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=NowI 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=NowI 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=Nowby 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 Whilebut 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.
-
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=NowI 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=NowI 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=Nowby 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 Whilebut 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.
I'd suggest doing the matches in phases so that you can track operator presedence:
and
is higher presendence thanor
: In fact you probably don't need to useRegex
,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 similarString.Split
on " or " -
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=NowI 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=NowI 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=Nowby 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 Whilebut 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.
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