Regular expression match words - ignores OR

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
LVL 1
rito1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MichaelStaszewskiCommented:
Try using this as your regex instead.

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rito1Author Commented:
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
käµfm³d 👽Commented:
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
MichaelStaszewskiCommented:
I'm not a C# guy and use PCRE mostly. There are differences between regex engines. That's good you found a solution.
0
käµfm³d 👽Commented:
There's a nice comparison of features between the two (and others) on this page:  http://www.regular-expressions.info/refflavors.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.