I’m trying to parse a simple filter condition using regular expressions
The filter should have the following syntax:
field=value
field:value
field!=value
field<>value
etc... (you get the idea)
I came up with the following (in scala)
val FilterEntry = """^(\w+)(!?)(=|:|<=|>=|<>|<|>)(.*)$""".r
val FilterEntry(v1, v2, v3, v4) = "field!<>value"
v1: String = field
v2: String = !
v3: String = <>
v4: String = value
So it’s a fine start
Now I’d like the regular expression to catch (raise an error) when no value is passed
I tried with this (I made the last group non optional)
val FilterEntry = """^(\w+)(!?)(=|:|<=|>=|<>|<|>)(.+)$""".r
val FilterEntry(v1, v2, v3, v4) = "field!<>"
v1: String = field
v2: String = !
v3: String = <
v4: String = >
So the problem is that it recognizes the operator as “<” and th value as “>” instead of correctly recognizing the operator as “<>” and the value as “” (If I test it with the firest reg exp, it correctly recognizes the operator as <> and the value as “”)
I guess I should tell the regular expression to match the operator greedily, but I thought it was like that by default…
— edit
I’ve just found this scala console online, for you the test the regular expression
http://www.simplyscala.com/
—
The are greedy, yes, but that does not mean they may not back track. You need a possessive quantifier for that.
Try this one:
^(\w+)(!?)(=|:|<=|>=|<>|<|>){1}+(.+)$Note that the order of the alternations in the operators start to matter… If the
<alternative appears before<=and<>then the match engine will always greedily match<first and then not even attempt to match the longer<=or<>options. If you just sort your alternatives by length from longest to shortest you should never have this problem.