473,785 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(o bject sender, System.EventArg s e)
{

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

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

}

private void button1_Click(o bject sender, System.EventArg s e)
{

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

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

}
Nov 16 '05 #1
3 1717
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" <sp*******@erst ads.com> wrote in
news:30******** *************** ***@posting.goo gle.com...
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(o bject sender, System.EventArg s e)
{

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

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

}

private void button1_Click(o bject sender, System.EventArg s e)
{

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

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

}

Nov 16 '05 #2
On 2004-08-20, Sped Erstad <sp*******@erst ads.com> wrote:

Regex reg=new Regex("[0-9]");
if (reg.IsMatch(te xtBox1.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(t extBox1.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.
Nov 16 '05 #3
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 <df*****@woofix .local.dom> wrote in message news:<slrncidji 7.8i3.df*****@w oofix.local.dom >...
On 2004-08-20, Sped Erstad <sp*******@erst ads.com> wrote:

Regex reg=new Regex("[0-9]");
if (reg.IsMatch(te xtBox1.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(t extBox1.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.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2307
by: Paul Chiusano | last post by:
I've been playing around with generators and have run into a difficulty. Suppose I've defined a Node class like so: class Node: def __init__(self, data=None, left=None, right=None): self.children = self.children.append(left) self.children.append(right) self.data = data
5
1833
by: Dr John Stockton | last post by:
ISTM that RegExps deserve a FAQ entry, with links to more detailed sources. An important question, probably not treated by many otherwise worthwhile sources, must be on feature detection of the newer RegExp facilities - for example, greedy/non-greedy. The answer may be that it is not possible to do so in a safe manner; that one can do no better than something like
26
13328
by: rkleiner | last post by:
Is there a regular expression to find the first unmatched right bracket in a string, if there is one? For example, "(1*(2+3))))".search(/regexp/) == 9 Thanks in advance.
19
3579
by: Dr Clue | last post by:
I'm not really an expert with RegExp() , although I do use it. The problem I have is that I want to strip comments out of a CSS file using RegExp() The reason is that I'm loading and parsing to simulate javscript access to stylesheets in Opera. I thought I had it licked untill the '/' characters in url('') tripped me up Below is a test case. I've tried many things. but if I can't figure out a nice clean RegExp(), I'm going to have to...
8
3350
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the || operator is before the && operator? e,g, In an expression like a = (z>x) || (x<0) && (z-y>9); is it guaranteed that z>x will be checked first?
29
3577
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this one data field - but i'm not sure) :-) Background info:
3
11514
by: VK | last post by:
If it was already answered somewhere, I'll be glad to be pointed to (after the necessary comments on my search abilities :-) I need as booletproof as possible way to strip out whitespaces from between tag borders in the source code. 1) left border defined by gt sign > 2) right border defined by lt sign < 3) If the content between left and right borders consists only of white spaces it has to be removed.
11
2936
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g; document.write( ); </script></body></html> ---------------------
5
1793
by: Ryan Krauss | last post by:
I need to parse the following string: $$\pmatrix{{\it x_2}\cr 0\cr 1\cr }=\pmatrix{\left({{{\it m_2}\,s^2 }\over{k}}+1\right)\,{\it x_1}-{{F}\over{k}}\cr -{{{\it m_2}\,s^2\,F }\over{k}}-F+\left({\it m_2}\,s^2\,\left({{{\it m_2}\,s^2}\over{k}}+1 \right)+{\it m_2}\,s^2\right)\,{\it x_1}\cr 1\cr }$$ The first thing I need to do is extract the arguments to \pmatrix{ } on both the left and right hand sides of the equal sign, so that the...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.