Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regex to match strings that don't contain any digits?

Reply
Thread Tools

regex to match strings that don't contain any digits?

 
 
Dave
Guest
Posts: n/a
 
      08-09-2005
On Tue, 09 Aug 2005 06:57:28 -0700, n_o_s_p_a__m decided we needed to
hear:

> Hello,
>
> I cannot think how to do this. I need to check if a string does not
> contain any digits. I tried the following, which didn't seem to work:
>
> [^\d]
> (?!\d)+
>
> Any suggestions?
>
> Thanks,
> KJ


/^[^0-9]+$/ or /^[^0-9]*$/ (if you want to allow empty string)
--
Dave <(E-Mail Removed)>
(Remove REMOVE for email address)

 
Reply With Quote
 
 
 
 
n_o_s_p_a__m@mail.com
Guest
Posts: n/a
 
      08-09-2005
Hello,

I cannot think how to do this. I need to check if a string does not
contain any digits. I tried the following, which didn't seem to work:

[^\d]
(?!\d)+

Any suggestions?

Thanks,
KJ

 
Reply With Quote
 
 
 
 
Arne Ruhnau
Guest
Posts: n/a
 
      08-09-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I cannot think how to do this. I need to check if a string does not
> contain any digits. I tried the following, which didn't seem to work:
>
> [^\d]
> (?!\d)+
>
> Any suggestions?


Untested: /^\D+$/

Arne Ruhnau
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-09-2005
(E-Mail Removed) wrote:
> Hello,
>
> I cannot think how to do this. I need to check if a string does not
> contain any digits. I tried the following, which didn't seem to work:
>
> [^\d]


This will check to see if the string contains any single non-digit at
all.

> (?!\d)+


This will check to see that the next thing is not a digit.

You need to reverse your thinking. Rather than checking if the string
does not contain any digits, check to see that it does contain entirely
non-digits.

$string =~ /^\D+$/

Paul Lalli

 
Reply With Quote
 
n_o_s_p_a__m@mail.com
Guest
Posts: n/a
 
      08-09-2005
That works great, thanks. Taking the example further, I'd like to find
out which strings do not match a particular regex.

For example, I want all the strings that do not match the following
expression (for a year range):

\d{2,}-\d+

How to do this? Negative lookahead?

 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      08-09-2005
(E-Mail Removed) writes:
> That works great, thanks.


Er, what works great? The article you're replying to has not yet
arrived on my news server, so I have no idea what solution you liked.
Please quote enough context that those of us (the majority, I suspect)
not using Google Groups to read USENET can figure out what you're
replying to.

> Taking the example further, I'd like to find
> out which strings do not match a particular regex.
>
> For example, I want all the strings that do not match the following
> expression (for a year range):
>
> \d{2,}-\d+
>
> How to do this? Negative lookahead?


Perldoc perlop, look for !~

Personally, though, I find !~ slows me down significantly when reading
a program. I don't know why, and I'm perfectly willing to believe it
is because I am a bear of very little brain, but every time I see it,
I mentally end up reading the regex, inverting it, and applying that
inversion to the string. Ugly, I know.

In general, I prefer to negate the test instead of the operator.
Instead of:

if ($string !~ /some regex/) {
...
}

I prefer:

unless ($string =~ /some regex/) {
...
}

-=Eric
 
Reply With Quote
 
n_o_s_p_a__m@mail.com
Guest
Posts: n/a
 
      08-09-2005
(responding to eric: The previous suggestions of /^[^0-9]+$/ or
/^[^0-9]*$/ or /^\D+$/ all "worked great".)

 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      08-09-2005
(E-Mail Removed) wrote:
> I cannot think how to do this. I need to check if a string does not
> contain any digits. I tried the following, which didn't seem to work:


> [^\d]
> (?!\d)+


> Any suggestions?


The regular expression metasymbol \D stands for any non-digit
character.

Using this you should be able to build a regex which excludes all
digits.

Axel


 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      08-10-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:
>
> I cannot think how to do this. I need to check if a string does not
> contain any digits.



In addition to the other replies; you can use the !~ operator to
determine if a string DOESN'T match a regex:

if ( $string !~ /\d/ ) {
print "$string does not contain any digits\n";
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
RCR: String#contain? and Array#contain? Roger Pack Ruby 3 09-28-2010 04:13 PM
Does string contain A, and if so, does a section of string contain B Jason Carlton Javascript 11 12-08-2009 06:07 PM
regex does not contain - or no match Sergio Ruiz Ruby 2 02-17-2009 03:41 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments