Help with Regular Expression

C

carlos

I am working on a regular expression validation for my search page.
What I have so far works for most cases, but I would like to fine tune
it some. I am new to regular expressions, and I do not have the time
to read up some more on it. Can someone help?

What I would like to do is allow words to be parsed using quotes.
However, they can also include boolean searching. Lastly, I need to
ensure the character's do not exceed a certain length. The expression
I have works, but if I put quotes on the first and not on the second
it should fail, but it is passing?? See below

Regex regex = new Regex("(?i)" + "(?:" + "\\" + "s*OR" + "\\" +
"s*)?" + "\"" + "([^" + "\"" + "]*)" + "\"");
passed = regex.IsMatch(value);

How the strings should pass:
"test" "this" "search"

"test" OR "this"

"this" AND "that"

This is passing, but should fail
"test" akfjafkajfakfj

Any suggestions?
 
J

Jesse Houwing

Hello Carlos,
I am working on a regular expression validation for my search page.
What I have so far works for most cases, but I would like to fine tune
it some. I am new to regular expressions, and I do not have the time
to read up some more on it. Can someone help?

What I would like to do is allow words to be parsed using quotes.
However, they can also include boolean searching. Lastly, I need to
ensure the character's do not exceed a certain length. The expression
I have works, but if I put quotes on the first and not on the second
it should fail, but it is passing?? See below

Regex regex = new Regex("(?i)" + "(?:" + "\\" + "s*OR" + "\\" +
"s*)?" + "\"" + "([^" + "\"" + "]*)" + "\"");
passed = regex.IsMatch(value);
How the strings should pass:
"test" "this" "search"
"test" OR "this"

"this" AND "that"

This is passing, but should fail
"test" akfjafkajfakfj
Any suggestions?

The expression you've posted is very hard to read, mostly due to all the
plussing in there. You can use verabatim strings to make them more readable.

I haven't tried why the expression isn't working. It doesn't seem to be complete
as it should match "AND" according to your examples, but there's no AND in
the regex anywhere.

The following expression does the trick:

^"[^"]+"((\s+((?i:AND|OR)\s+)?)"[^"]+")*$

And it's probably easier to use the verbatim string notation, than to escape
everything.

@"^""[^""]+""((\s+((?i:AND|OR)\s+)?)""[^""]+"")*$"

I'll try to explain the expression I used:

Start of the string: ^
Start with a quoted word: "[^"]+"
(
That is followed by one or more spaces: ((\s+
which is optionally followed by AND or OR followed by a one or more spaces:
((?i:AND|OR)\s+)?
which is followed by a new quoted word: "[^"]+"
) repeat group zero or more times
followed by the end of the string: $

this would allow:
"test" "test"
"test" AND "test" OR "test"
"test" "test" AND "test"
"test"

will not match:
AND OR
AND "test"
"test" AND
test
test "test"
test AND "test"

I wan't exactly sure if you would allow "test"AND"test", your old expression
looks like it would work. This expression would do just that:
^"[^"]+"((\s+|\s*(?i:AND|OR)\s*)"[^"]+")*\r?$

My guess is that it would be easier/faster to use Regex.Split and split on
"AND|OR" and then check if all the strings in the result are quoted once
you trim them.

Jess
 
T

tomisarobot

regex takes a while to get used to doesn't it :)

"(\"[^\"]+\"|OR|AND)+ "

would be my approach. i am assuming my syntax is wrong, but here is
my thought process:

find a quote, skip anything that isn't a quote. when i have a quote
thats the end of my phrase. \"[^\"]+\"
OR is a phrase
AND is a phrase

(search phrase|or phrase|and phrase) are all phrases.

find as many space seperated phrases as i can.


I never remember regex unless i sit there and play with it though, so
assume that what i gave you is horribly broken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top