我可以这样做吗?
...blah-blah...AA_word.......blah-blah...AB_word......word........Nword....
我只需要替换那些不以“AB_”开头的单词出现,所以看起来像这样:
...blah-blah...AA_XXXX.......blah-blah...AB_word......XXXX........NXXXX....
这可能吗?我或多或少熟悉正则表达式,但仍然无法找到如何做到这一点。编程语言无关紧要 - php,js或c#,无论
答案 0 :(得分:3)
搜索:(?<!AB_)word
并替换为:XXXX
perl实现:
my $str = "...blah-blah...AA_word.......blah-blah...AB_word......word........Nword....";
$str =~ s/(?<!AB_)word/XXXX/g;
say $str;
<强>输出:强>
...blah-blah...AA_XXXX.......blah-blah...AB_word......XXXX........NXXXX....
使用php:
$str = "...blah-blah...AA_word.......blah-blah...AB_word......word........Nword....";
$str = preg_replace('/(?<!AB_)word/', 'XXXX', $str);
echo "$str\n";
答案 1 :(得分:0)
在JS中:
var s = "...blah-blah...AA_word.......blah-blah...AB_word......word........Nword...."
s.replace("AB_word", "????").replace(/word/g, "XXXX").replace("????", "AB_word")
// results in
// ...blah-blah...AA_XXXX.......blah-blah...AB_word......XXXX........NXXXX....
HTH