in reply to /g option not making s// find all matches
In the spirit of TIMTOWTDI, I offer another solution to my own problem.
After using the tips above to solve this problem, I moved on to the next one and, in the process, ran across a section of code employing a solution offered to another question I had asked here three years ago. And it turns out the solution I used in that case would have solved this current issue as well. Apparently, even the small fraction of perl that I know is too large to all fit into my brain at once.
The substitution line in my original example could becomeThis separates the test for tilde from the substitution action (conceptually akin to tybalt89's solution and Eily's first one), yet keeps the whole thing inside a single s/// operator. And however much I admire the cleverness of \G (?! \A), its meaning is obscure even after its clockwork has been explained, whereas the above line is fairly easy to parse even if you've never encountered the @{[...]} idiom before.s/^ ~ .* /@{[ ${^MATCH} =~ tr#_#+#r ]}/xp;
For thoroughness, I plugged this s/// into AnomalousMonk's more comprehensive test suite above, and it passed all those as well.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: /g option not making s// find all matches
by AnomalousMonk (Archbishop) on May 31, 2018 at 20:14 UTC | |
by raygun (Scribe) on Jun 01, 2018 at 04:11 UTC |