|
|
|
start date: Thu, 09 Aug 2007 19:49:39 -0000,
posted on: microsoft.public.dotnet.framework.aspnet
back
| Thread Index |
|
1
carlos
|
|
2
Jesse Houwing
|
|
3
unknown
|
|
4
unknown
|
Help with Regular Expression
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?
Date:Thu, 09 Aug 2007 19:49:39 -0000
Author:
|
Re: Help with Regular Expression
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.
Jesse
Date:Thu, 9 Aug 2007 21:38:04 +0000 (UTC)
Author:
|
Re: Help with Regular Expression
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
Date:Thu, 09 Aug 2007 14:40:58 -0700
Author:
|
Re: Help with Regular Expression
You can't split on AND.
"I love my clown AND my honda" AND "cheddar"
Date:Fri, 10 Aug 2007 11:59:56 -0700
Author:
|
|
|