icoder has asked for the wisdom of the Perl Monks concerning the following question:

I tried to pull out a signed /or unsgined number from string using regexp in perl.

$str = "This string has a number +15 init "; regex : m/.*([\+|-](\d+)).*$/ #which works well for above said +string

but consider this case

$str = "This string has a number15 init"; #i need to fix the above regex to work for avove string as well

Replies are listed 'Best First'.
Re: pull out num from str
by aaron_baugher (Curate) on Apr 23, 2012 at 07:50 UTC

    First of all, a character class already means "one of these characters", so your pipe isn't doing what you think it is. It would match |123. So that should be [+-]. It's also not necessary to escape the plus sign within a character class. Then, to say "zero or one of these," append ? to it, giving you [+-]? . You can also drop the .* from the beginning and .*$ from the end, since they're implied anyway. That gets your regex down to /([+-]?\d+)/ .

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      You can also drop the .* from the beginning

      "123 456" =~ /([+-]?\d+)/ and "123 456" =~ /.*([+-]?\d+)/ match two different things because .* at the beginning is greedy.

      Thanks . works exactly as expected
      This works fine , Is there any disadvantages of using this regex,because ,others in this thread posted lenthy regexes which will do the same thing. please suggest

        If it works with all the formats your data includes, that's what's important. As jwkrahn pointed out, by removing your leading .*, I did change the effect of yours in any case where there is more than one matching number in a string:

        $str = 'This sentence has two numbers, +13 and -15.'; $str =~ /([+-]?\d+)/; # matches +13, first match found $str =~ /.*([+-]?\d+)/; # matches -15 because .* is greedy

        So which of those you use depends on whether you want to find the first number or the last number in a line. If your lines only have one number, it won't matter.

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

Re: pull out num from str
by Anonymous Monk on Apr 23, 2012 at 07:10 UTC

    Regexp::English

    #!/usr/bin/perl -- use Regexp::English; print Regexp::English ->new ->remember ->multiple ->digit, "\n"; __END__ (?^:((?:\d+)))
      I can't use any packages here
        I can't use any packages here
        Strange rule, but than simply paste the package code into your main package...

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: pull out num from str
by JavaFan (Canon) on Apr 23, 2012 at 11:20 UTC
    $ perl -MRegexp::Common -E 'say $RE{num}{int}' (?:(?:[+-]?)(?:[0123456789]+))
      In what ways the above regex is better than this one ? m/([+-]?\d+)/ suggestion needed
        Well, it's "better" in the sense that it doesn't match on a sequence of digits thrown together from a couple of handful of different scripts.

        But I posted it mainly to point you to the Regexp::Common module. Useful if you need patterns for common tasks, but you aren't able to write them yourself without asking on the internet.

Re: pull out num from str
by 2teez (Vicar) on Apr 23, 2012 at 09:12 UTC

    On both strings the below regex works:

    regex: m{.+?([+-]?[[:digit:]]+?)\s+?.+?};