我对regex很陌生,所以我不知道该怎么做。仅供参考,我正在使用Python,但我不确定这有多重要。你知道吗
我想做的是这样的:
string1 = 'Metro boomin on production wow'
string2 = 'A loud boom idk why I chose this as an example'
pattern = 'boom'
result = re.sub(pattern, ' ____ ', string1)
result2 = re.sub(pattern, ' ____ ', string2)
现在那会给我"Metro ____in on production wow"
和"a loud ____ idk why I chose this as an example
我想要的是"Metro ______ on production wow"
和"a loud ____ idk why I chose this as an example"
基本上我想在另一个字符串中找到一个目标字符串,然后将匹配的字符串和2个空格之间的所有内容替换为一个新字符串
有办法吗?如果可能,最好根据原始字符串的长度在替换字符串中使用可变长度
你在正确的轨道上。只要扩展一下你的正则表达式。你知道吗
而且
\S*
符号匹配零个或多个非空格的元素。你知道吗要用相同数量的下划线替换文本,请指定lambda回调,而不是替换字符串:
相关问题 更多 >
编程相关推荐