Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Help with Perl RE

by kennethk (Abbot)
on Jan 22, 2009 at 16:16 UTC ( [id://738210]=note: print w/replies, xml ) Need Help??


in reply to Help with Perl RE

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2025-04-20 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.