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");
}
}
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");
}
}