Regexp Question: Two Nots Makes a Right to Left?

S

Sped Erstad

There must be a simple regexp reason for this little question but it's
driving me nuts. Below is a simple regexp to determine if a string
contains only numbers. I'm running these two strings through the two
very subtly different pieces of code: "0" and "0a"

If I do the two "nots" on it, it works perfectly ("0" succeeds, "0a"
fails). However, if I get rid of the two "nots", it appears to not do
a global match properly on the string: "0" still succeeds, "a" fails,
but "0a" also succeeds!

Am I missing something simple? Most of the examples I see use the two
"nots" which is not a problem at all - just understanding it would be
nice!

Thanks,
Sped

private void button1_Click(object sender, System.EventArgs e)
{

Regex reg=new Regex("[0-9]");
if (reg.IsMatch(textBox1.Text) )
{
MessageBox.Show("Success!");

}
else
{
MessageBox.Show("Failed");
}

}





private void button1_Click(object sender, System.EventArgs e)
{

Regex reg=new Regex("[^0-9]");
if (!reg.IsMatch(textBox1.Text) )
{
MessageBox.Show("Success!");

}
else
{
MessageBox.Show("Failed");
}

}
 
N

Niki Estner

Regex.Match is similar to String.IndexOf: It will search for any appearance
of the search pattern within the string; That is, searching for "[0-9]" will
match the first digit in a string. So Regex.IsMatch("This string contains 1
digit", "[0-9]") will return true.

If I got you right you want to check if *every* char in the string is a
digit: To do that you will need the meta-characters ^ and $, which mean
"start of string" and "end of string", respectively. Matching for "^[0-9]+$"
will do the test you want.

Niki
 
D

David

Regex reg=new Regex("[0-9]");
if (reg.IsMatch(textBox1.Text) )

This means, does any character in this string match a digit. In other
words, you're looking for a digit ("[0-9]"), and then you're asking "Is
there a match of a digit anywhere in the string?"
Regex reg=new Regex("[^0-9]");
if (!reg.IsMatch(textBox1.Text) )

Now, we're looking through the entire string for anything that is NOT a
digit. If we didn't match anything at all, then the string must contain
ONLY digits. [1]

I can see where the double negative not cancelling can throw you, but
consider that we can express the same notion in English.

Is it true that there's a digit in this string?
vs.
Is it false that there's a non-digit in this string?

To be slightly pedantic, the ambiguity appears because falseness and
inversion aren't really the same concept, it's really a trick of
language that makes of think of both of them as being "NOT".

[1] unless of course it's the empty string, but the regex covers that
case.
 
S

Sped Erstad

Funny how it makes more sense on a Saturday morning than on a Friday
at 4:00 - definitely not pedantic at all... Sometimes it helps to take
a few steps back to see the bigger picture. It definitely makes sense
now!

Thanks for the responses...


David said:
Regex reg=new Regex("[0-9]");
if (reg.IsMatch(textBox1.Text) )

This means, does any character in this string match a digit. In other
words, you're looking for a digit ("[0-9]"), and then you're asking "Is
there a match of a digit anywhere in the string?"
Regex reg=new Regex("[^0-9]");
if (!reg.IsMatch(textBox1.Text) )

Now, we're looking through the entire string for anything that is NOT a
digit. If we didn't match anything at all, then the string must contain
ONLY digits. [1]

I can see where the double negative not cancelling can throw you, but
consider that we can express the same notion in English.

Is it true that there's a digit in this string?
vs.
Is it false that there's a non-digit in this string?

To be slightly pedantic, the ambiguity appears because falseness and
inversion aren't really the same concept, it's really a trick of
language that makes of think of both of them as being "NOT".

[1] unless of course it's the empty string, but the regex covers that
case.
 

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