Regexp Question: Two Nots Makes a Right to Left? | | |
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");
}
} | | | | re: Regexp Question: Two Nots Makes a Right to Left?
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
"Sped Erstad" <spedlists@erstads.com> wrote in
news:308d1f4e.0408201349.65011512@posting.google.c om...[color=blue]
> 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");
> }
>
> }[/color] | | | | re: Regexp Question: Two Nots Makes a Right to Left?
On 2004-08-20, Sped Erstad <spedlists@erstads.com> wrote:[color=blue]
>
> Regex reg=new Regex("[0-9]");
> if (reg.IsMatch(textBox1.Text) )[/color]
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?"
[color=blue]
> Regex reg=new Regex("[^0-9]");
> if (!reg.IsMatch(textBox1.Text) )[/color]
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. | | | | re: Regexp Question: Two Nots Makes a Right to Left?
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 <dfoster@woofix.local.dom> wrote in message news:<slrncidji7.8i3.dfoster@woofix.local.dom>...[color=blue]
> On 2004-08-20, Sped Erstad <spedlists@erstads.com> wrote:[color=green]
> >
> > Regex reg=new Regex("[0-9]");
> > if (reg.IsMatch(textBox1.Text) )[/color]
>
> 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?"
>[color=green]
> > Regex reg=new Regex("[^0-9]");
> > if (!reg.IsMatch(textBox1.Text) )[/color]
>
> 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.[/color] |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|