Solved

Regular expression match words - ignores OR

Posted on 2012-03-22
5
430 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 74

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 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now