By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

[Regular Expression] (aaa AND bbb) OR (ccc AND ddd)

P: n/a
teo

I need to implement a boolean evaluation
in a Regular Expression like this:
(aaa AND bbb) OR (ccc AND ddd)
(see the #3 case)
- - -

1)
If I need to match a single word only,
no problem:

MyString = "hallo"
MioMatch = Regex.Match(MyBigText, MyString)

2)
If I need to match a couple of words in OR relation,
I think I have to use the 'pipe' |
in this way:

MyString = "dog|cat"
MioMatch = Regex.Match(MyBigText, MyString)

3) here the problem

If I need to match
a couple of words (in AND relation),
and evaluate them in OR relation
with another couple of words (in AND relation),
like this:

(dog AND cat) OR (milk AND coffe)

How can I achieve this ?

This obviously doesn't work:
MyString = "(dog AND cat) OR (milk AND coffe)"
MioMatch = Regex.Match(MyBigText, MyString)

Nov 3 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Click the link below for all your RegEx needs :)

http://www.google.com/search?client=...=Google+Search
Nov 3 '06 #2

P: n/a
teo
On Fri, 3 Nov 2006 14:13:48 -0000, "Robinson"
<to******************@myinboxtoomuchtoooften.comwr ote:
>Click the link below for all your RegEx needs :)

http://www.google.com/search?client=...=Google+Search
I've found nothing about matching
two words at the same time
or
two other words at the same time

Nov 3 '06 #3

P: n/a
teo,
MyString = "(dog AND cat) OR (milk AND coffe)"
Are you saying you want to find both dog & cat in a string or both milk &
coffe in a string?

Does dog have to precede cat or can it follow cat?
What about interleaving characters?
Some RegEx resources:

Expresso:
http://www.ultrapico.com/Expresso.htm

RegEx Workbench:
http://www.gotdotnet.com/Community/U...aspx?SampleGui...

A tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/de...ary/en-us/cpge...

Expresso & RegEx Workbench are helpful tools for learning regular
expressions & testing them.

I use the regular-expressions.info as a general regex reference, then fall
back to MSDN for the specifics. The above link is .NET 1.x; I don't have the
..NET 2.0 link handy; not sure if any thing changes in 2.0.
--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"teo" <te*@inwind.itwrote in message
news:1a********************************@4ax.com...
>
I need to implement a boolean evaluation
in a Regular Expression like this:
(aaa AND bbb) OR (ccc AND ddd)
(see the #3 case)
- - -

1)
If I need to match a single word only,
no problem:

MyString = "hallo"
MioMatch = Regex.Match(MyBigText, MyString)

2)
If I need to match a couple of words in OR relation,
I think I have to use the 'pipe' |
in this way:

MyString = "dog|cat"
MioMatch = Regex.Match(MyBigText, MyString)

3) here the problem

If I need to match
a couple of words (in AND relation),
and evaluate them in OR relation
with another couple of words (in AND relation),
like this:

(dog AND cat) OR (milk AND coffe)

How can I achieve this ?

This obviously doesn't work:
MyString = "(dog AND cat) OR (milk AND coffe)"
MioMatch = Regex.Match(MyBigText, MyString)
Nov 4 '06 #4

P: n/a
teo
>teo,
>MyString = "(dog AND cat) OR (milk AND coffe)"
Are you saying you want to find both dog & cat in a string or both milk &
coffe in a string?
that's right

>Does dog have to precede cat or can it follow cat?
this is not important now, both the cases are good

(anyway if there is a way or parameter
to set the precedence it will be surely useful in the future...)
>What about interleaving characters?
what do you mean with 'interleaving' characters?

I never met this word (I'm Italian)

>Some RegEx resources:
Thanks, I previously went deep on MSDN fx 2.0
and on the renowed VBNet book by F.Balena,
and on the shareware version of 'RegEx Buddy '
(Now REgExBuddy is expired so I'll go with Expresso )

but found tons of samples about one or more chars,
but NO sample about one or more words and their AND or OR
boolean evaluation

(I met only the OR operand like dog|cat ,
but it is not my case)
The implementation of that simple boolean evaluation
like (dog AND cat) OR (milk AND coffe)
still remains...
(maybe the interleaving chars could help,
but what are those? )
Nov 5 '06 #5

P: n/a
Teo,
>>Does dog have to precede cat or can it follow cat?
this is not important now, both the cases are good
(anyway if there is a way or parameter
to set the precedence it will be surely useful in the future...)
setting the precedence generally means you doubled the number of expressions
as now you need:

MyString = "(dog AND cat) OR (cat AND dog)"

>>What about interleaving characters?
what do you mean with 'interleaving' characters?
I never met this word (I'm Italian)
It means characters between the words.

In other words would "I had milk in my coffe" or "it rained cats and dogs"
match?

AND is simply listing the characters:

dogcat

However that says there are no interleaving characters (no characters)
between the words.

To introduce interleaving characters you need:

dog.*cat

or

dog.+cat

The first says dog followed by any character zero or more times followed by
cat. While the second says dog followed by any character one or more times.

Instead of . I would consider \s which says white space.

OR is simply alternation:

dog|milk

Will match a string with dog it in or milk in it.
You should be able to use a string such as (untested):

MyString = (dog.*cat)|(cat.*dog)|(milk.*coffe)|(coffe.*milk)

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"teo" <te*@inwind.itwrote in message
news:9t********************************@4ax.com...
teo,
>>MyString = "(dog AND cat) OR (milk AND coffe)"
Are you saying you want to find both dog & cat in a string or both milk &
coffe in a string?

that's right

>>Does dog have to precede cat or can it follow cat?

this is not important now, both the cases are good

(anyway if there is a way or parameter
to set the precedence it will be surely useful in the future...)
>>What about interleaving characters?

what do you mean with 'interleaving' characters?

I never met this word (I'm Italian)

>>Some RegEx resources:

Thanks, I previously went deep on MSDN fx 2.0
and on the renowed VBNet book by F.Balena,
and on the shareware version of 'RegEx Buddy '
(Now REgExBuddy is expired so I'll go with Expresso )

but found tons of samples about one or more chars,
but NO sample about one or more words and their AND or OR
boolean evaluation

(I met only the OR operand like dog|cat ,
but it is not my case)
The implementation of that simple boolean evaluation
like (dog AND cat) OR (milk AND coffe)
still remains...
(maybe the interleaving chars could help,
but what are those? )
Nov 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.