in reply to regex replacement help
sub add_line_break { my (@out)=@_; for (@out) { s/(~+)/$1\n/g; } return wantarray ? @out :$out[0]; }
A logical change I would make (and a slight efficiency boost) would be to change that to a substitution applied to a location preceded by a tilde but not followed by a tilde:
s/(?<=~)(?!~)/\n/g;
That uses a positive look-behind and a negative look-ahead (see Looking ahead and looking behind in perlretut).
As a side note, when you want to understand what a regular expression does, check out YAPE::Regex::Explain. It can be very useful when learning regular expressions and when dealing with unfamiliar/old code.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: regex replacement help
by Eliya (Vicar) on Mar 29, 2011 at 19:22 UTC | |
|
Re^2: regex replacement help
by mkenney (Beadle) on Mar 29, 2011 at 19:09 UTC |