从末端找到字符串中的第一匹匹配

发布于 2025-02-13 04:12:10 字数 641 浏览 1 评论 0原文

我有一个字符串

Manager of Medical Threat Devlop at Micro

,我想找到 ,的 的任何单词。在这里,我想获得['micro'](在 word的最后一个之后,在字符串的末尾)。

当前代码

如果我应用r'(?:for | ot | of)\ s+(。*)'我会得到不正确的[ micro']

更多示例

  • canno - > CANNO
  • Medicalof threant devlop的经理 - > CANNO
  • Medical For Thraist DevLop的经理 - > Canno
  • threat devlop在Canno Matt - > Canno Matt

I have a string

Manager of Medical Threat Devlop at Micro

I want to find any words that go after at, for, of. Here, I want to get the ['Micro'] (that is at the end of string, after the last at word).

Current code

If I apply r'(?:for|at|of)\s+(.*)' I will get incorrect ['Medical Threat Devlop at Micro'].

More examples:

  • 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一念一轮回 2025-02-20 04:12:10

您可以使用

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

regex demo 详细信息

  • 。* - 除线断裂字符以外的任何零或更多字符,尽可能多的
  • \ b - 一个Word Boundare
  • (?:for | at | of) - for , at 或的
  • \ s+ -一个或多个空格
  • (。*) - 第1组:除折扣以外的任何零或更多字符,尽可能多。

另一个将获得相同结果的正则是

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

详细信息

  • \ b - word boundard boundary
  • (?for | AT | of) - 对于, 或
  • \ s+ - 一个或多个whitepsepaces
  • ((?:(?) ,或的 的总体 yat a 尽可能多的事件, 整个单词char sequence
  • $ 代码> - 字符串的结尾。

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

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

详细信息

  • \ b - word boundard boundary
  • (?for | AT | of) - 对于, 或
  • \ s+ - 一个或多个whitepsepaces
  • ((?:(?),或的 的总体 yat a 尽可能多的事件, 整个单词char sequence
  • $ 代码> - 字符串的结尾。

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

You can use

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

See the regex demo. Details:

  • .* - any zero or more chars other than line break chars, as many as possible
  • \b - a word boundary
  • (?:for|at|of) - for, at or of
  • \s+ - one or more whitespaces
  • (.*) - Group 1: any zero or more chars other than line break chars, as many as possible.

Another regex that will fetch the same results is

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

Details:

  • \b - a word boundary
  • (?:for|at|of) - for, at or of
  • \s+ - one or more whitespaces
  • ((?:(?!\b(?:for|at|of)\b).)*) - Group 1: any char, other than a line break char, zero or more but as many as possible, occurrences, that does not start a for, at or of as a whole word char sequence
  • $ - end of string.

Note you can also use re.search since you expect a single match:

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

Details:

  • \b - a word boundary
  • (?:for|at|of) - for, at or of
  • \s+ - one or more whitespaces
  • ((?:(?!\b(?:for|at|of)\b).)*) - Group 1: any char, other than a line break char, zero or more but as many as possible, occurrences, that does not start a for, at or of as a whole word char sequence
  • $ - end of string.

Note you can also use re.search since you expect a single match:

离旧人 2025-02-20 04:12:10

尝试此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中都不擅长。(但是我明白了)


是的。


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

print(string.replace(*s,''))

Try this re.split would work this.

Your question is not fully clear give some more input and output examples.

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

THERE IS ANOTHER METHOD TO DO THIS (USING 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:])

I am not good in re at all.(But I get it)


Yeah there is another to do this.(Using re.findall)


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

print(string.replace(*s,''))
转瞬即逝 2025-02-20 04:12:10

如果您想用正则表达式执行此操作,那么这就是这样做的方法。

用空字符串替换以下等级的匹配:

.*\b(?:for|at|of)\b\s?

这将匹配:

  • *:任何字符(从本质上,此模式都尽可能匹配
  • )对于| at | of)\ b :边界符号之间的热词
  • \ s?:可选空间

检查演示在这里

If you want to do it with a regex, then here's the way to do it.

Replace matches of the following regex with the empty string:

.*\b(?:for|at|of)\b\s?

This will match:

  • .*: any character (by its nature, this pattern will match as most characters as possible)
  • \b(?:for|at|of)\b: your hotwords between boundary symbols
  • \s?: an optional space

Check the demo here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文