overlapping find and replace

Chris Jack chris_jack at msn.com
Tue Oct 19 13:41:09 BST 2010


On 18 Oct 2010, at 16:11, Michael Lush wrote:
> I have a string ABCDEFGH and want to highlight two overlapping hits
> BCDE and DEFG in HTML to make A<b>BC<i>DE</b>FG</i>H
> 
> The obvious $string =~ s{(BCDE|DEFG)}{<b>$1</b>}g; does not work as the 
> modified string doesn't match the second query and I don't get differnet
> fonts for each overlapping match.
> 
> Is there a conventional way of doing this?

It really depends on what your general case is. For your specific case, you could consider the fairly simple:

$string =~ s{(BC)(DE)(FG)}{$1<b>$2</b>$3}g;
 
For a more general case: I would suggest you pre-parse the LHS of the RE to achieve something similar.
 
Regards
Chris 		 	   		  


More information about the london.pm mailing list