APM: replacing tab separated blanks

Mike Stok mike at stok.co.uk
Wed Dec 4 12:16:43 CST 2002


On Wed, 4 Dec 2002, Tom.Bakken wrote:

> I want to replace the following:
> 
> TabTabTabTab
> 
> with:
> 
> Tab0Tab0TAb0
> 
> when I use s/TabTab/Tab0Tab/g
> 
> it only substitutes every other 0 because it begins searching AFTER the
> last pattern match.
> Is there anything like a gsub or tricky regular expression I could use
> to avoid splitting each line and testing each element?

To put 0 between every Tab you can use (?=Tab) to do a zero width 
assertion e.g.

[mike at won mike]$ perl -de 1

Loading DB routines from perl5db.pl version 1.19
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1):   1
  DB<1> $s = 'TabTabTabTab'

  DB<2> $s =~ s/(Tab)(?=Tab)/${1}0/g

  DB<3> print $s
Tab0Tab0Tab0Tab
  DB<4> 

But I still have an extra 'Tab' on the end - your desired result was 
Tab0Tab0Tab0.  If you want to get rid of that then you could just

  s/Tab$//;

afterwards.

Hope this helps,

Mike

-- 
mike at stok.co.uk                    |           The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA 
mike at exegenix.com                  | Fingerprint      0570 71CD 6790 7C28 3D60
http://www.exegenix.com/           |                  75D2 9EC4 C1C0 0599 13DA




More information about the Austin mailing list