我正在尝试使用 Perl 将文件中的某些字符串与正则表达式匹配,该正则表达式将用等量的 X 个字符替代它们,与原始字符串长度一样长。 例如,该文件可能包含如下内容:
"the quick brown hello world fox jumps over the world" etc. etc.
和一个字典,例如包含如下字符串:"hello world"
并且我之前会加载到一个数组中。
我想得到以下结果:
"the quick brown XXXXX XXXXX fox jumps over the world" etc. etc.
最佳答案
没有。
但是,您的语言可能具有接受回调的正则表达式替换函数。然后你可以这样做:
>>> re.sub(r'o+b', lambda m: 'x' * len(m.group(0)), 'foobar')
'fxxxar'
关于regex - 有没有办法用相同数量的 X 个字符替换子字符串的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31007476/