gpt4 book ai didi

python - 正则表达式匹配一个特定的单词后跟另一个单词,以及一个边界标识符

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:30 25 4
gpt4 key购买 nike

我正在用 Python 进行正则表达式匹配。我尝试遵循一些组合但没有用。我对正则表达式完全陌生。我的问题是,我的字符串如下。

string = ''' World is moving towards a particular point'''

我想要一个解决方案来检查单词“towards”是否紧跟在“moving”之后,如果是,我想选择该行的其余部分(在“towards”之后),直到它以“.”结尾。或“-”。我是新手。请提供一些好的建议。

最佳答案

有点像

re.findall (r'(?<=moving towards )[^-.]*', string)
['a particular point']
  • (?<=moving towards )看看断言的背后。断言字符串前面有 moving towards

  • [^-.]*匹配除 - 以外的任何内容或 .


如何匹配

World is moving towards a particular point
|
(?<=moving towards ) #checks if this position is presceded by moving towards
#yes, hence proceeds with the rest of the regex pattern

World is moving towards a particular point
|
[^-.]

World is moving towards a particular point
|
[^-.]

# and so on


World is moving towards a particular point
|
[^-.]

Regex Demo

关于python - 正则表达式匹配一个特定的单词后跟另一个单词,以及一个边界标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27729179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com