从末端找到字符串中的第一匹匹配
我有一个字符串
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
regex demo 。 详细信息:
。*
- 除线断裂字符以外的任何零或更多字符,尽可能多的\ b
- 一个Word Boundare(?:for | at | of)
- for , at 或的\ s+
-一个或多个空格(。*)
- 第1组:除折扣以外的任何零或更多字符,尽可能多。另一个将获得相同结果的正则是
详细信息:
\ b
- word boundard boundary(?for | AT | of)
-对于
, 或\ s+
- 一个或多个whitepsepaces((?:(?)
,或的 的总体 yat a 尽可能多的事件, 整个单词char sequence$ 代码> - 字符串的结尾。
注意,您还可以使用
re.Search
,因为您期望一个匹配:You can use
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
orof
\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
Details:
\b
- a word boundary(?:for|at|of)
-for
,at
orof
\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 afor
,at
orof
as a whole word char sequence$
- end of string.Note you can also use
re.search
since you expect a single match:尝试此
re.split
可以使用此操作。您的问题尚不完全清楚,给出了更多的输入和输出示例。
输出
还有另一种方法可以执行此操作(使用
re.finditer
)。我在
re
中都不擅长。(但是我明白了)是的。
Try this
re.split
would work this.Your question is not fully clear give some more input and output examples.
OUTPUT
THERE IS ANOTHER METHOD TO DO THIS (USING
re.finditer
).I am not good in
re
at all.(But I get it)Yeah there is another to do this.(Using
re.findall
)如果您想用正则表达式执行此操作,那么这就是这样做的方法。
用空字符串替换以下等级的匹配:
这将匹配:
*
:任何字符(从本质上,此模式都尽可能匹配\ 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:
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 spaceCheck the demo here