473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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?

Aug 9 '07 #1
4 1603
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

Aug 9 '07 #2
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
Aug 9 '07 #3
You can't split on AND.

"I love my clown AND my honda" AND "cheddar"

Aug 10 '07 #4
On Aug 10, 1:59 pm, tomisaro...@gmail.com wrote:
You can't split on AND.

"I love my clown AND my honda" AND "cheddar"
Thank you all so much for the help! Works perfect

Aug 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
6
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
6
by: deepak_kamath_n | last post by:
Hello, I am relatively new to the world of regex and require some help in forming a regular expression to achieve the following: I have an input stream similar to: Slot: slot1 Description:...
14
by: Chris | last post by:
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = Can anybody help me out? Thanks for any help!
3
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.