Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: substitution in regular expression

by trizen (Hermit)
on Apr 23, 2014 at 22:15 UTC ( #1083476=note: print w/replies, xml ) Need Help??


in reply to substitution in regular expression

One-line solution: "ABCDEF" =~ /([A-Z]{3})(?{print "$1\n"})(?!)/;

Replies are listed 'Best First'.
Re^2: substitution in regular expression
by aeqr (Novice) on Apr 23, 2014 at 22:27 UTC
    Thanks for the idea, I'll write it down. Just one thing, could you explain the:
    (?{print "$1\n"})(?!)
    I don't understand the question mark before the print block. Also why the (?!) at the end. I noticed that removing it only prints ABC, but I don't understand why. Thank you

      Short explanation: (?{...}) means to execute arbitrary Perl code inside a regular expression, and (?!) makes the regex engine to fail and backtrack, trying to match from the last_pos + 1. When it starts matching ABC, it prints it, fails, backtracks and starts matching from B the next three letters, giving us BCD. The process repeats until the internal regex counter reaches the end of the string.

      I know, I'm really bad at explaining things to humans, but, fortunately, Athanasius explained this better once.

      Please see: Re: RegEx + vs. {1,}

        Thanks a lot! Very well explained actually. It's a nice trick to know.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1083476]
help
Chatterbox?
[LanX]: I told you the story about the “new" currency trading system of Dresdner Bank
[LanX]: ?
[Corion]: No, or I don't remember the story ;)
LanX my favourite don't tell me the proof of concept is good enough for production anecdote
[ambrus]: ah, it's one of thos
[Eily]: what, there's a difference between proof of concept and production?
[LanX]: 20 years ago traders were complaining about the latency of the trading system...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2017-03-29 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (349 votes). Check out past polls.