正则表达式中的可选点 [英] Optional dot in regex

查看:55
本文介绍了正则表达式中的可选点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想用 Mister 替换 Mr.Mr 的所有匹配项.

我正在使用以下正则表达式:\bMr(\.)?\b 来匹配 Mr. 或仅匹配 Mr.然后,我使用 re.sub() 方法进行替换.

令我困惑的是,它正在用 Mister. 取代 Mr..为什么要在末尾保留点 . ?看起来它不匹配 Mr\. 案例,而只是 Mr.

导入重新s="a rMr.Nobody Mr.Nobody 是 MrNobody 和 MraNobody.re.sub(r"\bMr(\.)?\b","先生", s)

返回:

'a rMr.没有人先生.没有人是无名先生和无名先生.

我也尝试了以下方法,但也没有运气:

re.sub(r"\b(Mr\.|Mr)\b","Mister", s)

我想要的输出是:

'a rMr.无人先生无人先生是无人先生和无人先生.^ ^没有点这应该保持原样

解决方案

我认为您想捕获 'Mr' 后跟 一个 '.'字边界:

r"\bMr(?:\.|\b)"

正在使用中:

<预><代码>>>>进口重新>>>re.sub(r"\bMr(?:\.|\b)", "Mister", "a rMr.Nobody Mr.Nobody 是 MrNobody 和 MraNobody.")'先生.无人先生无人先生是无人先生和无人先生.

Say I want to replace all the matches of Mr. and Mr with Mister.

I am using the following regex: \bMr(\.)?\b to match either Mr. or just Mr. Then, I use the re.sub() method to do the replacement.

What is puzzling me is that it is replacing Mr. with Mister.. Why is this keeping the dot . at the end? It looks like it is not matching the Mr\. case but just Mr.

import re
s="a rMr. Nobody Mr. Nobody is Mr Nobody and Mra Nobody."
re.sub(r"\bMr(\.)?\b","Mister", s)

Returns:

'a rMr. Nobody Mister. Nobody is Mister Nobody and Mra Nobody.'

I also tried with the following, but also without luck:

re.sub(r"\b(Mr\.|Mr)\b","Mister", s)

My desired output is:

'a rMr. Nobody Mister Nobody is Mister Nobody and Mra Nobody.'
                     ^                              ^
                     no dot            this should be kept as it is

解决方案

I think you want to capture 'Mr' followed by either a '.' or a word boundary:

r"\bMr(?:\.|\b)"

In use:

>>> import re
>>> re.sub(r"\bMr(?:\.|\b)", "Mister", "a rMr. Nobody Mr. Nobody is Mr Nobody and Mra Nobody.")
'a rMr. Nobody Mister Nobody is Mister Nobody and Mra Nobody.'

这篇关于正则表达式中的可选点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆