1

I'm trying to parse apart the different filter strings for an sql server WHERE clause, and I thought that regular expressions would be the way to go. Fortunately, the filter will always be flat, there will be no "sub-filters". Each filter statement will always be surrounded in parenthesis. Here's an example of a string I'd like to parse:

([IsActive]=(1)) AND ([NoticeDate] >= GETDATE()) AND (NoticeDate <= DATEADD(day, 90, GETDATE()))

The result I'd like is an array with the following items:

[0] = ([IsActive]=(1)) AND
[1] = ([NoticeDate] >= GETDATE()) AND
[2] = (NoticeDate <= DATEADD(day, 90, GETDATE()))

The closest I've come is the following regex:

/\(.+?\) (and|or)/i

but this only returns

[0] = ([IsActive]=(1)) AND
[1] = AND

So basically I'd like to return anything surrounded in parenthesis, followed by a space, followed by the string "and" or "or". The last statement will not be followed by and/or, though I could concatenate a " and" string if that would make it easier. This is being done using classic asp JScript. I've pretty much exhausted my (weak) regular expression abilities, any help would be greatly appreciated. Thanks in advance

3 Answers 3

3

Try this regex:

(?i)\(.+?\)(?:\s+(?:and|or)|$)

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

@WillP., It is ignore-case option. Try use /i instead.
I am only getting the first one returning using cyber-reality.com/regexy.html
@WillP., Try this JavaScript tester: regexpal.com. It gives correct result.
0

If your WHERE clauses are free-form, /\(.+?\) (and|or)/i will spuriously match inside tokens like strings and comments.

Consider

SELECT resting_place FROM Nation
WHERE date_founded LIKE "Four score and seven years ago"
AND conceived IN (liberty)
ORDER BY the_people

The and in "Four score and seven years" is not a SQL keyword, but a single regular expression approach is not going to be able to distinguish keyword uses from non-keyword uses.

Comments

0

A more robust way would probably be to do a proper parser. This should be doable with a classic LL(1) parser. There are plenty of parser generators around. In fact, one can easily consider regular expressions to be a kind of parser generator, too.

Anyway, for this kind of tasks, a proper LL(1) parser is likely the better idea. It would also be able to support nested terms, if you want.

https://en.wikipedia.org/wiki/LL_parser

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.