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

D

Dave

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)
 
N

n_o_s_p_a__m

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
 
A

Arne Ruhnau

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
 
P

Paul Lalli

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.

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
 
N

n_o_s_p_a__m

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?
 
E

Eric Schwartz

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
 
N

n_o_s_p_a__m

(responding to eric: The previous suggestions of /^[^0-9]+$/ or
/^[^0-9]*$/ or /^\D+$/ all "worked great".)
 
A

axel

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
 
D

Dave Weaver

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

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top