Say I have a string in Perl I am trying to match and replace with stuff:
$string =~ s/[^a-zA-Z]$find[^a-zA-Z]/$replace/g;
So as shown, I want to replace everything that is surrounded on both sides by nonletter characters. However, when I replace the string, I do NOT want to also replace these characters: they are just necessary for correct matching. How can I tell the Perl regex to avoid replacing the things surrounding $find
?
Use perl lookaround assertions.
s/(?<=[^a-zA-Z])$find(?=[^a-zA-Z])/$replace/g