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

Discussion in 'Perl Misc' started by Dave, Aug 9, 2005.

  1. Dave

    Dave Guest

    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 <>
    (Remove REMOVE for email address)
    Dave, Aug 9, 2005
    #1
    1. Advertising

  2. Dave

    Guest

    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
    , Aug 9, 2005
    #2
    1. Advertising

  3. Dave

    Arne Ruhnau Guest

    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
    Arne Ruhnau, Aug 9, 2005
    #3
  4. Dave

    Paul Lalli Guest

    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
    Paul Lalli, Aug 9, 2005
    #4
  5. Dave

    Guest

    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?
    , Aug 9, 2005
    #5
  6. 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
    Eric Schwartz, Aug 9, 2005
    #6
  7. Dave

    Guest

    (responding to eric: The previous suggestions of /^[^0-9]+$/ or
    /^[^0-9]*$/ or /^\D+$/ all "worked great".)
    , Aug 9, 2005
    #7
  8. Dave

    Guest

    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
    , Aug 9, 2005
    #8
  9. Dave

    Dave Weaver Guest

    <> 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";
    }
    Dave Weaver, Aug 10, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    628
  2. Sergio Ruiz

    regex does not contain - or no match

    Sergio Ruiz, Feb 17, 2009, in forum: Ruby
    Replies:
    2
    Views:
    146
    Sergio Ruiz
    Feb 17, 2009
  3. Roger Pack
    Replies:
    3
    Views:
    147
    Roger Pack
    Sep 28, 2010
  4. Frank Skare
    Replies:
    2
    Views:
    203
    Jim Cochrane
    Apr 17, 2004
  5. Jason Carlton
    Replies:
    11
    Views:
    233
    Dr J R Stockton
    Dec 8, 2009
Loading...

Share This Page