parsing a filter condition with regular expressions

I'm trying to parse a simple filter condition using regular expressions

The filter should have the following syntax:

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



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.

Need Your Help

polygon tessellation in opengl shader

opengl shader

I want 20k+ 2D polygons to be rendered in opengl with outline &amp; fill.

Problems moving camera in unity

c# camera unity3d

My problem is I want to point my camera towards the ground but still move it around horizontally. The problem is when I move the camera forwards, because it has been pointed at an angle towards the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.