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

Reg Expression translation

by Anonymous Monk
on Nov 14, 2003 at 19:25 UTC ( [id://307166]=perlquestion: print w/replies, xml ) Need Help??

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

Please help me understand the below reg expressions:
/^\s*$/
means check for all spaces in the entry. Check for beginning spaces and spaces in the middle and ending with a space??
/^\w{1,}@/
Check for beginning with one word only and a '@' ??

Replies are listed 'Best First'.
Re: Reg Expression translation
by danger (Priest) on Nov 14, 2003 at 19:39 UTC
    Please help me understand the below reg expressions:
    /^\s*$/
    means check for all spaces in the entry. Check for beginning spaces and spaces in the middle and ending with a space??

    It means: match the beginning of the string, followed by zero or more whitespace characters, followed by the end of the string. Essentially, it matches what we often think of as a "blank" line.

    /^\w{1,}@/
    Check for beginning with one word only and a '@' ??

    Match beginning of string, followed by 1 or more word characters (those being of the class: [a-zA-Z0-9_]) followed by the '@' character.

    To really understand regular expressions better, please see the following documents: perlrequick, perlretut, perlre. Also, Jeffrey Friedl's "Mastering Regular Expressions" is an excellent resource.

Re: Reg Expression translation
by Anonymous Monk on Nov 14, 2003 at 19:52 UTC

    Use YAPE::Regex::Explain.

    $ perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(q(^\ +s*$))->explain' The regular expression: (?-imsx:^\s*$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- $ perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(q(^\ +w{1,}@))->explain' The regular expression: (?-imsx:^\w{1,}@) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- \w{1,} word characters (a-z, A-Z, 0-9, _) (at least 1 times (matching the most amount possible)) ---------------------------------------------------------------------- @ '@' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
Re: Reg Expression translation
by Albannach (Monsignor) on Nov 14, 2003 at 19:54 UTC
    Provided you do read perlre and the other fine references so that you have a basis for understanding, you might find YAPE::Regex::Explain (by our own japhy) to be of use.

    perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(shift)->explain;" "^\s*$"

    yields

    The regular expression: (?-imsx:^\s*$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    --
    I'd like to be able to assign to an luser

Re: Reg Expression translation
by fletcher_the_dog (Friar) on Nov 14, 2003 at 19:54 UTC
    /^\s*$/ is a common way of seeing if a string is empty or has just white space. Basically it matches if from the beginning ("^") to the end ("$") where there is nothing or only white space ("\s*") between.
    /^\w{1,}@/ is just another way of writing /^\w+@/ which matches one or more "word" characters ("\w+") followed by "@" at the beginning of the line ("^")
      thanks to everyone.
Re: Reg Expression translation
by meetraz (Hermit) on Nov 14, 2003 at 20:10 UTC
    One thing you can try is YAPE-Regex-Explain. It works like this:

    use strict; use YAPE::Regex::Explain; my $regex1 = qr/^\s*$/; my $regex2 = qr/^\w{1,}@/; print YAPE::Regex::Explain->new($regex1)->explain; print YAPE::Regex::Explain->new($regex2)->explain;

    (Its a cool module from japhy)

    UPDATE: Whoops, looks like everybody beat me to it!

Re: Reg Expression translation
by hardburn (Abbot) on Nov 14, 2003 at 19:31 UTC

    perlre

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-19 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found