Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Regex help needed

by davido (Cardinal)
on Apr 23, 2012 at 10:28 UTC ( [id://966572]=note: print w/replies, xml ) Need Help??


in reply to Regex help needed

print "$+{first} $+{last}\n" if m/^ (?<last> [^%]+ ) % (?<first> [^%]+ ) % (?<id> A [^%]+ ) /x;

Really, the critical component is to use a negated character class to match anythig that is not the delimiter, rather than relying on non-greedy specifications.

If your Perl is old enough to not have named captures, this is equivilent code that is more universally compatible:

print "$2 $1\n" if m/^([^%]+)%([^%]+)%(A[^%]+)/;

Update: Added /x and nicer formatting.


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-19 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found