![]() |
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) |
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 |
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 |
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 |
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? |
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 |
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".) |
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 |
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 02:40 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.