D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] perl substitutions

 

On Thu, Apr 10, 2008 at 1:53 PM, Thomas Arrow wrote:
> I have no idea if perl works the same way as sed but if so placing a 'g' after
>  the last slash should make it do more than one edit per line.

It does work the same as sed (roughly), but the g won't help you here.
It has the effect of trying to match again, but won't start at the
beginning of the string. Instead it will start at the end of the
previously matched pattern. So
  $a =~ s/_/ /g
will successfully replace all underscored by a space, but the regex
  $a =~ s/\[\[(.*)_(.*)\]\]/\]\]$1 $2\]\]/
searches for two square left brackets, then something, then an
underscore, then something and then two right brackets. After these
right brackets, there are no such matches anymore, so the regex will
only match once. If perl wouldn't work this way, a regex like s/_/__/g
would result in an endless loop. Luckily, the regex will return true
if and only it has replaced something, so one can use a while loop
with an empty 'body'.

Martijn.

-- 
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/linux_adm/list-faq.html