http://www.perlmonks.org?node_id=151892


in reply to About regular expression

First, you're missing a ~ character. It should be: -- Not necessary. There's an implicit match against $_.--geb

(code deleted)

Second, let's break it down:

my ($label) =~ / # begin \. # find a literal . ( # start capturing [^\.] # Any character that isn't a \ or . * # zero or more ) # stop capturing $ # only match from the end of the string / # end;
In a nutshell this will match the "extension" of the data you provided, i.e. the "five" in "one.five" or the "12" in "two.ten.12". What's matched is captured into $label.

There's lots of info on regexes both here and in the regular Perl documentation. They can be challenging at first, but are well worth the effort to figure out. :-D

Gary Blackburn
Trained Killer

Edited

Replies are listed 'Best First'.
Re: Re: About regular expression
by abstracts (Hermit) on Mar 15, 2002 at 03:08 UTC
    Sorry, but I don't think there is a missing '~'. $label is assigned the matched between the parens (everything after the dot). It's not being matched by the re.

    Aziz,,,

      Yeah, you're right. There's an implicit match against $_ there that I didn't see. I guess it just wasn't implied enough. :-D

      Gary Blackburn
      Trained Killer

Re: Re: About regular expression
by Juerd (Abbot) on Mar 15, 2002 at 21:13 UTC

    [^\.] # Any character that isn't a \ or .

    Not exactly. The backslash is used as an escape character, not only in the regex itself, but in character classes too. Even though it's not necessary at all to escape a dot in a character class, Perl removes the backslash itself (Perl always does something when you use a backspace in an interpolated string, unlike some languages where "\q\n" is backslash, q, newline. In Perl, "\q\n" is q, newline.).

    [^\.] # Any character that is not . [^.] # Any character that is not .

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk