Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Help with Perl RE

by newbie2009 (Initiate)
on Jan 22, 2009 at 16:03 UTC ( [id://738202]=perlquestion: print w/replies, xml ) Need Help??

newbie2009 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to Perl. Can you please help me understand what the following line means?
if($line =~ /^([^{};]+[{};])(\s*\S+.*)$/s) { ## Do something here }

Thank you in advance.
--Andy

Replies are listed 'Best First'.
Re: Help with Perl RE
by kennethk (Abbot) on Jan 22, 2009 at 16:16 UTC

    The syntax

    $line =~ //

    is short hand for

    $line =~ m//

    and checks if the contents of variable $line match the pattern surrounded by the //. Details on regular expressions can be found in the documentation in Perl regular expressions, quick reference guide, and tutorial. The pattern to be matched is

    ^([^{};]+[{};])(\s*\S+.*)$

    and uses the s modifier, which tells the regex engine to treat the whole string as 1 line. The pattern does the following, in order:

    1. ^Start at the start of the string
    2. [^{};]+Match at least 1 of the characters other than {,} and ; (^ is not)
    3. [{};]Match exactly 1 of the characters {,} and ;
    4. \s*Match any amount of whites space (including none)
    5. \S+Match at least one non-white space character
    6. .*Match any number of any character
    7. $Stop at the end of the string

    ([^{};]+[{};]) also stores the punctuation in the variable $1 and (\s*\S+.*) stores the rest of the string in $2. If the string matches, then the conditional is true and the if block is executed.

    Update: Cleaned up the above and put it in list form.

    Update 2: I'm an idiot. When inside [], a carat means not. Thanks, cdarke.

Re: Help with Perl RE
by toolic (Bishop) on Jan 22, 2009 at 17:23 UTC
    There is also a tool to help understand Perl regular expressions: YAPE::Regex::Explain. It will at least explain what is inside the regex delimeters.
    use warnings; use strict; use YAPE::Regex::Explain; my $re = '^([^{};]+[{};])(\s*\S+.*)$'; print YAPE::Regex::Explain->new($re)->explain();
Re: Help with Perl RE
by johngg (Canon) on Jan 22, 2009 at 23:46 UTC

    A couple of useful features are the ability to pre-compile a regular expression, storing it in a variable for later use and the ability to use extended syntax to break the pattern up with whitespace and comments to make it more readable. Here is your pattern given such treatment.

    my $rxLine = qr {(?xs) # Use extended regular expression syntax (x) # allowing whitespace in the pattern for # readability, and comments. Allow the . # (regexp wildcard metacharacter) to match # a newline (s) ^ # Anchor pattern to start of string ( # Open capture group (1st) [^{};]+ # Negated character class, anything except {, } # or ; characters. + quantifier is 1 or more # of, greedy matching so will match as many # as possible [{};] # Character class, any of {, } or ; characters. # No quantifier so exactly 1 of ) # Close capture group (1st) ( # Open capture group (2nd) \s* # Whitespace character. * quantifier is 0 or more # of, greedy \S+ # Non-whitespace character. 1 or more, greedy .* # Any character (including newline because of the # s modifier). 0 or more, greedy ) # Close capture group (2nd) $ # Anchor pattern to end of string }; if( $line =~ $rxLine ) { # Do something here }

    Have a look at the documentation links provided by kennethk for explanations of things like character classes and greedy versus non-greedy quantifiers.

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Help with Perl RE
by Grey Fox (Chaplain) on Jan 22, 2009 at 17:59 UTC
    Hi Andy;
    I had come across a great tool for explaining regexe's called My REGEX Tester. I have a link to it in Found new online Regex tool.
    -- Grey Fox
    "We are grey. We stand between the darkness and the light" B5
Re: Help with Perl RE
by Discipulus (Canon) on Jan 23, 2009 at 08:23 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://738202]
Approved by lidden
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2025-03-17 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (54 votes). Check out past polls.