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

T

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)
 
J

Jay B. Harlow

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/UserSamples/Details.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/default.asp?url=/library/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.
 
T

teo

teo,
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? )
 
J

Jay B. Harlow

Teo,
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 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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top