替换match python中的特定子字符串

2024-04-24 19:17:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我要用regex替换df序列中匹配字符串的子字符串。 我浏览了文档(例如HERE),找到了一个能够捕获我想要匹配的特定类型字符串的解决方案。但是,在替换过程中,它不会替换子字符串。你知道吗

我有这样的案子

data
initthe problem
nationthe airline
radicthe groups
professionthe experience
the cat in the hat

在这种特殊情况下,我感兴趣的是在“the”不是独立字符串的情况下用“al”替换“the”(即前面加空格,后面加空格)。你知道吗

我尝试了以下解决方案:

patt = re.compile(r'(?:[a-z])(the)')
df['data'].str.replace(patt, r'al')

但是,它也替换了“the”前面的非空白字符。你知道吗

有什么建议我可以做的只是重新计算这些子字符串的具体情况吗?你知道吗


Tags: the字符串文档类型dfdatahere过程
1条回答
网友
1楼 · 发布于 2024-04-24 19:17:52

尝试使用lookback,它检查(断言)在the之前的字符,但实际上不消耗任何内容:

input = "data\ninitthe problem\nnationthe airline\nradicthe groups\nprofessionthe experience\nthe cat in the hat"

output = re.sub(r'(?<=[a-z])the', 'al', input)
print(output)

data
inital problem
national airline
radical groups
professional experience
the cat in the hat

Demo

相关问题 更多 >