in reply to Matching lines in a file that end in numbers (was: regular expression)

Your regular expression saves everything in the first set of parenthesis. Your first regex does /(.*)\d+$/. Since .* is greedy, that grabs everything up to the last number and puts it in $1.

I don't understand from your question what you're trying to print, exactly. If you don't want to print any of the numbers, use: /(.*?)\d+/. That will make the first part of your regular expression non-greedy. This means it will match as little as possible before stopping.

If that's not what you're looking for, please clarify.

HTH HAND

Replies are listed 'Best First'.
Re: Re: regular expression
by Anonymous Monk on Feb 28, 2001 at 11:30 UTC
    Yes, I do not want to print any number appearing after regular code. I agree with your code and looking exactly for. I totally forgot about ?. After coming home from office I did figure out in anotherway.
    /(.*)([^\d].*)$/
    I found from the C source code that I am compiling will not having any code after the numbers in the end and the above reg. exp. is safe. But I should admit that your approach is elegant. Thanks again Ashok
      What I would suggest is:     print "$1\n" if (/^(.*?)\s*\d+$/); This will have the effect of removing the trailing space from the line.

      I'm not sure if this will be an issue for your application or not, but your program might get a bit carried away if you (or someone else) spaced out some of your expressions, such as:
      x = ReallyLongFunctionNameNumber1() + 130 + AnotherCrazyLongFunctionNameThatIsOnItsOwnLine();
      I would presume that you don't want the program to pick up on that line, in which case, a slightly safer version might be to put a semi-colon in your regexp, like so:     print "$1\n" if (/^(.*?;)\s*\d+$/); Or to demand a specific quantity of numbers, such as 5:     print "$1\n" if (/^(.*?)\s*\d{5}$/); Although, to be truly "safe", you would want to change the format of your numerical markup system slightly, such as turning it into a comment, like:     int i; // #20005 Where '// #20005' is not very likely to show up anywhere else in your code.

      Though, of course, this will depend on the context of your application, and it might be way over-kill.