in reply to Skipping special tags in regexes

Another possibility is to not rely on regexes. I'm not sure what your specs for searching and replacing are, but if they are narrowed down to prefixing words with other words, as in your example, you could get away with building a custom function and passing it the line to change, the word to look for, and what to prefix it with, e.g.:

#!/usr/bin/perl -w use strict; sub prefix { my ($line, $look_for, $prefix, $flip) = @_; return $line unless $look_for && $line =~ /$look_for/; my @arr = split /(<[^>]*>)/, $line; for my $cnt (0..$#arr) { if ($arr[$cnt] =~ /$look_for/) { $arr[$cnt-2] .= $prefix if $cnt > 1 && !$flip; $arr[$cnt+2] = $prefix . ' ' . $arr[$cnt+2] if $cnt < $#ar +r-1 && $flip; } } return join '', @arr; } my $line = "<5b>I <5c>like <5d>tacos\n"; print prefix ($line, 'tacos', 'yummy'); print prefix ($line, 'like', 'yummy', 1); __OUTPUT__ <5b>I <5c>like yummy<5d>tacos <5b>I <5c>like <5d>yummy tacos

Replies are listed 'Best First'.
Re: Re: Skipping special tags in regexes
by fletcher_the_dog (Friar) on Dec 04, 2003 at 21:04 UTC
    unfortunately, I have many regexes that are not fixed strings and require all the powers of regexes