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

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

Replies are listed 'Best First'.
Re: Regular Expression (Fix)
by tadman (Prior) on Feb 28, 2001 at 20:08 UTC
    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.