从末尾开始查找字符串中的第一个匹配项



我有一个字符串

Manager of Medical Threat Devlop at Micro

我想找到at,for,of后面的任何单词。在这里,我想获得['Micro'](即在字符串的末尾,在最后一个at单词之后)。

<<p>

当前代码/strong>如果我应用r'(?:for|at|of)s+(.*)',我会得到不正确的['Medical Threat Devlop at Micro']

更多示例:

  • Manager of Medical Threat Devlop at Canno->Canno
  • Manager of Medicalof Threat Devlop of Canno->Canno
  • Manager of Medicalfor Threat Devlop for Canno->Canno
  • Threat Devlop at Canno Matt->Canno Matt

可以使用

re.findall(r'.*b(?:for|at|of)s+(.*)', text)

参见regex演示。细节:

  • .*-除换行符外的任何零个或多个字符,尽可能多
  • b- a字边界
  • (?:for|at|of)-for,atof
  • s+-一个或多个空白
  • (.*)-组1:除换行符外的任何零个或多个字符,尽可能多。

另一个获取相同结果的正则表达式是

re.findall(r'b(?:for|at|of)s+((?:(?!b(?:for|at|of)b).)*)$', text)

细节:

  • b- a字边界
  • (?:for|at|of)——foratof
  • s+-一个或多个空白
  • ((?:(?!b(?:for|at|of)b).)*)-第1组:任何字符,除了换行字符,零或更多,但尽可能多,不开始for,atof作为一个完整的字字符序列
  • $- end of string.

注意您还可以使用re.search因为你期望一个匹配:

match = re.search(r'.*b(?:for|at|of)s+(.*)', text)
if match:
print(match.group(1))

试试这个re.split就可以了

你的问题不是很清楚,再给一些输入和输出的例子。

import re
s = 'Manager of Medical Threat Devlop at Micro'
s = re.split(r'at |for |of ',s)[-1:]
print(s)
输出
IN                         :  OUTPUT
'Manager of Medical Threat Devlop at Micro' : ['Micro']
'Threat Devlop at Canno Matt'               : ['Canno Matt']

有另一种方法可以做到这一点(使用re.finditer)。

import re
string = 'Threat Devlop at Canno Matt'
s = re.finditer(r'(at | for | of )',string,)
last_index = list(s)[-1].end()
print(string[last_index:])

我根本不擅长re。(但我明白了)


是的,还有另一种方法。(使用re.findall)


import re
string = 'Threat Devlop at Canno of Matjkasa'
s = re.findall(r'.*(?:at|for|of)s+', string)
print(string.replace(*s,''))

如果你想用正则表达式来做,那么这里是这样做的。

用空字符串替换以下正则表达式的匹配项:

.*b(?:for|at|of)bs?

这将匹配:

  • .*:任何字符(根据其性质,此模式将匹配尽可能多的字符)
  • b(?:for|at|of)b:你的边界符号之间的热词
  • s?:可选空间

点击这里查看演示

相关内容

最新更新