in reply to Re: pull out num from str
in thread pull out num from str

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

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

    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.