Syntactic Confectionery Delight | |
PerlMonks |
Re: Search and replace regex, but only retain a portion of the string (updated x4)by AnomalousMonk (Archbishop) |
on Dec 22, 2018 at 22:51 UTC ( [id://1227620]=note: print w/replies, xml ) | Need Help?? |
I think I even want to get to UK_Mobile_Vodafone_GBRVF. Assuming that's really what you want:
Update 1: Or (with inspiration from tybalt89): Update 2: Or with no eval: With Perl version 5.10+, you could use a persistent state variable: state $xlate = [ '', '_' ]; (changing the replacement expression, of course). (The original code of this update had a no warnings 'uninitialized'; statement which turned out to be unneeded. Removed.) Update 3: Or another two-stepper:
Update 4: Or: I'm going to bed now. (Update: I don't know why I based this solution on an anonymous array; ('', '_')[! $1] is a bit less obscure and probably slightly faster. But beyond that, this eval-ed solution doesn't really offer anything more than the ternary expression used in the eval one-step in my original reply. Well, it was late...) Give a man a fish: <%-{-{-{-<
In Section
Seekers of Perl Wisdom
|
|