Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   regex to match strings that don't contain any digits? (http://www.velocityreviews.com/forums/t893666-regex-to-match-strings-that-dont-contain-any-digits.html)

Dave 08-09-2005 10:20 AM

Re: regex to match strings that don't contain any digits?
 
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 <dave@REMOVEbundook.com>
(Remove REMOVE for email address)


n_o_s_p_a__m@mail.com 08-09-2005 01:57 PM

regex to match strings that don't contain any digits?
 
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


Arne Ruhnau 08-09-2005 02:17 PM

Re: regex to match strings that don't contain any digits?
 
n_o_s_p_a__m@mail.com 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

Paul Lalli 08-09-2005 02:34 PM

Re: regex to match strings that don't contain any digits?
 
n_o_s_p_a__m@mail.com 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


n_o_s_p_a__m@mail.com 08-09-2005 05:00 PM

Re: regex to match strings that don't contain any digits?
 
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?


Eric Schwartz 08-09-2005 05:41 PM

Re: regex to match strings that don't contain any digits?
 
n_o_s_p_a__m@mail.com 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

n_o_s_p_a__m@mail.com 08-09-2005 06:09 PM

Re: regex to match strings that don't contain any digits?
 
(responding to eric: The previous suggestions of /^[^0-9]+$/ or
/^[^0-9]*$/ or /^\D+$/ all "worked great".)


axel@white-eagle.invalid.uk 08-09-2005 07:00 PM

Re: regex to match strings that don't contain any digits?
 
n_o_s_p_a__m@mail.com 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



Dave Weaver 08-10-2005 08:10 AM

Re: regex to match strings that don't contain any digits?
 
n_o_s_p_a__m@mail.com <n_o_s_p_a__m@mail.com> 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";
}




All times are GMT. The time now is 04:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.