Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: What does this regex do?

by davido (Cardinal)
on Sep 22, 2006 at 06:56 UTC ( [id://574336]=note: print w/replies, xml ) Need Help??


in reply to What does this regex do?

It should (assuming a match) capture a series of values from $file into the array @cvs. The matching occurs like this (explanation brought to you by YAPE::Regex::Explain):

The regular expression: (?s-imx:((?:(?:[^\n@]+|@[^@]*@)\n?)+)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?s-imx: group, but do not capture (with . matching \n) (case-sensitive) (with ^ and $ matching normally) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- (?: group, but do not capture (1 or more times (matching the most amount possible)): ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- [^\n@]+ any character except: '\n' (newline), '@' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- @ '@' ---------------------------------------------------------------------- [^@]* any character except: '@' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- @ '@' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- \n? '\n' (newline) (optional (matching the most amount possible)) ---------------------------------------------------------------------- )+ end of grouping ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

The preceeding explanation is the output from the following test code:

use warnings; use strict; use YAPE::Regex::Explain; my $REx = qr/((?:(?:[^\n@]+|@[^@]*@)\n?)+)/s; print YAPE::Regex::Explain->new($REx)->explain;

When deciphering a regular expression, it's often helpful to use the /x modifier so you can lay the regular expression out in smaller chunks that are easier to digest.

my $REx = qr/ ( (?: (?: [^\n@]+ | @ [^@]* @ ) \n? )+ ) /gsx;

Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found