Solved

Regular expression match words - ignores OR

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#, asp.net MVC, sql server, LINQ 3 32
ASP.NET MVC identity 6 27
Most efficient JavaScript way to verify if variable matches any value in the list? 6 23
Linq asp.net mvc 13 11
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

803 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