Regular Expression Problem

F

focus

Hi,

I'm wanting to return a match on a string using a regular expression
where the value contains certain words but also doesn't include
certain words.

My string looks like this:

Toyota Mazda Ford Honda Holden

I only want it to return a match if the string contains Toyota but
doesn't include Honda.

Any ideas on how to do this?

Thanks.

Craig.
 
J

Jon Skeet [C# MVP]

focus said:
I'm wanting to return a match on a string using a regular expression
where the value contains certain words but also doesn't include
certain words.

My string looks like this:

Toyota Mazda Ford Honda Holden

I only want it to return a match if the string contains Toyota but
doesn't include Honda.

Any ideas on how to do this?

The easiest way is not to use a regular expression:

if (myString.Contains ("Toyota") &&
!myString.Contains ("Honda"))

You'd need some more work to make it cope with word breaks etc, but you
could always apply that to your regex too - just apply two regular
expressions.

There may well be ways of doing it within a single regular expression,
but it's likely to be a lot less readable.
 
F

focus

The example I gave it a simple breakdown of what i'm trying to do.

I really want to be able to achieve this by using one regular
expression if possible.
 
F

focus

Also in the example above I assume that Toyota always comes before
Honda in the list.
 

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