Solved

Regular expression match words - ignores OR

Posted on 2012-03-22
5
444 Views
Last Modified: 2012-03-22
Hi All,

In C# I have the following variable that tests for the following matches... ' not ',' and ',' or '.

This seems to work fine when locating matches for ' not ' and ' and ' but it doesn't work when locating the keyword ' or '.

Can anyone see what I am doing wrong?

Match matchBoolOperators = Regex.Match(strSearchTerm.ToLower(), @"[ not ][ and ][ or ]");

Open in new window


Many thanks,

Rich
0
Comment
Question by:rito1
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
MichaelStaszewski earned 250 total points
ID: 37752415
Try using this as your regex instead.

(\s)(not|and|or)(\s)

Open in new window

0
 
LVL 1

Author Comment

by:rito1
ID: 37752563
Thanks MichaelStaszewski,

That didn't seem to work in my C#... I am starting to understand there their a different implementation of regexp for different languages... this seemed to work fine:

\bAND\b|\bNOT\b|\bOR\b

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 37752567
Can anyone see what I am doing wrong?
MichaelStaszewski's suggestion is the way to go. I'd like to add why.

You are using character classes in your regex. A character class works like a logical OR in regex. The caveat is that it works as an OR against the literal characters contained within it. In your case, what your pattern means is:

[ not ]  - Either a space, a "n", an "o", or a "t" (space is used twice, but as far as character classes go, repeated characters are treated as if they occurred only once)
[ and ]  - Either a space, an "a", a "n", or a "d" (space, see above)
[ or ]   - Either a space, an "o", or a "r" (space, see above

Open in new window


The effect of your pattern is that any of the following (and more) would match:

nar
nao
ndo
nnr
...
0
 
LVL 4

Expert Comment

by:MichaelStaszewski
ID: 37752591
I'm not a C# guy and use PCRE mostly. There are differences between regex engines. That's good you found a solution.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37752719
There's a nice comparison of features between the two (and others) on this page:  http://www.regular-expressions.info/refflavors.html
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question