in reply to Regex woes...

You regexp is correct except for the (-) part. The (-) requires there to be a - there and caputures it into $2. In this case the parens aren't really necessary. However, you probably want a ? after the -. The ? after something means that it may occur zero or one time.

That said, you might want to replace the (-) not with -? but with [-+]?. [-+] creates a character class matching - or +, so it would allow numbers like +157.0.

I think you probably want a $ or \z at the end of your regex to say that the end must be either the end of the string or a newline before it (for $), or the end of the string, no exceptions, (for \z.)

perlre is your friend, as always.

update: As adamsj pointed out you also used - instead of , with \d{1-3}. I read too fast to notice.

Replies are listed 'Best First'.
Re: Re: Regex woes...
by adamsj (Hermit) on Jul 09, 2001 at 03:55 UTC
    And that raises an interesting point, which probably has a trivial answer which I don't know. Why doesn't a construct like \d{1-3} make -w scream?

    Even more oddly, if it's reversed to say \d{3-1}, it neither makes -w scream nor does it DWIM and match, say, -12.3, like it would if it were \d{2}.

    adamsj

    They laughed at Joan of Arc, but she went right ahead and built it. --Gracie Allen

      -Mre=debug reveals the answer:

      ... Compiling REx `\d{3-1}' ... 1: DIGIT(2) 2: EXACT <{3-1}>(5) 5: END(0) ...

      Apparently this means the regex got compiled to match a digit, and then the literal text {3-1}. It would be nice if perl recognized that there might have been confusion with the {M,N} syntax, and thus would throw a warning, though issues of backwards compatiblity would need to be considered in doing this.

      If you look at regcomp.c, you'll see that when the regx parser finds an open brace, it looks for digits, and optionally followed by a comma and another optional string of digits. Anything else falls through as plaintext. (See the S_regpiece() function.)

      japhy -- Perl and Regex Hacker