Regular Expression Pattern

B

Ben

Hi,

I need to write a regex pattern that goes like this:
All the supplies strings, except a specific string.
So for example if I choose the string to operate on to be "Apple",
the pattern should return match for all strings except "Apple".
I need just the regex pattern, not an implementation in C#.

Thanks,
Ben
 
B

Ben

Ok.

Let's say I have an array of strings: {"Apple", "Banana", "Orange"}.
And suppose that I ask the user to enter a string.
I then iterate the array and if the current string in the iteration doesn't
match the string the user entered, I add it to another array.

I need the regex pattern to have a Not logic in it, or something like that.
 
M

Martin Honnen

Ben said:
Let's say I have an array of strings: {"Apple", "Banana", "Orange"}.
And suppose that I ask the user to enter a string.
I then iterate the array and if the current string in the iteration doesn't
match the string the user entered, I add it to another array.

I need the regex pattern to have a Not logic in it, or something like that.

The suggestion is not to build the "not matching" into the regular
expression but rather to put it into the C# code where you would do e.g.
if (!Regex.IsMatch(input, pattern))
 
B

Ben

Ahhhh, if it were that simple :)

I don't have the source code for the application.
It displays a list of values and I need to supply it with a regular
expression for not matching a specific value that I choose.
Well, I've played around with it a little and came up with this pattern:
(?!Apple).*
This seems to somehow work, but I need to also to be able to supply more
than one value. I tried (?!Apple).*|(?!Banana).* but it didn't work. I guess
I need to "And" them, not to "Or" them.

Any ideas?
 
G

Geoffrey Summerhayes

Ok.

Let's say I have an array of strings: {"Apple", "Banana", "Orange"}.
And suppose that I ask the user to enter a string.
I then iterate the array and if the current string in the iteration doesn't
match the string the user entered, I add it to another array.

I need the regex pattern to have a Not logic in it, or something like that.

Why do you need a regular expression?
Just use the string comparison functions.
 
B

Ben

It's a tool that I didn't write.
It accepts a regular expression pattern to do its search.
 
B

Ben

I guess i've found a solution.
If for example I want to include all strings but "Apple", I'd write the
following pattern:

(?!Apple).*

And if for example I want to include all but "Apple" and "Banana", I simply
need to use the pipe character '|', so I'd write the following:

(?!Apple|Banana).*

That's it (after two days of trial and error :) )
 
G

Geoffrey Summerhayes

I guess i've found a solution.
If for example I want to include all strings but "Apple", I'd write the
following pattern:

(?!Apple).*

And if for example I want to include all but "Apple" and "Banana", I simply
need to use the pipe character '|', so I'd write the following:

(?!Apple|Banana).*

That's it (after two days of trial and error  :) )

Applesauce
 

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