Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Regex help needed

by davido (Archbishop)
on Apr 23, 2012 at 10:28 UTC ( #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
Node Status?
node history
Node Type: note [id://966572]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2017-12-15 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (443 votes). Check out past polls.

    Notices?