[英]Perl string replace: Match, but not replace, part of regex
Say I have a string in Perl I am trying to match and replace with stuff: 假设我在Perl中有一个字符串,我想匹配并替换为以下内容:
$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
? 我怎样才能告诉Perl正则表达式避免替换$find
周围的东西?
将它们存储为匹配的组,并在替换字符串中引用它们:
$string =~ s/([^a-z])$find([^A-Z])/\1$replace\2/gi;
使用环顾断言 。
s/(?<=[^a-zA-Z])$find(?=[^a-zA-Z])/$replace/g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.