我正在用Python进行正则表达式匹配我试着遵循一些组合,但没有成功。我对正则表达式完全不熟悉。我的问题是,我有一个字符串如下。
string = ''' World is moving towards a particular point'''
我想要一个解决方案来检查单词“moving”之后是否立即出现单词“toward”,如果是,我想要选择行的其余部分(在“toward”之后),直到它以“.”或“-”结束我是新手。请提出一些好的建议。
最佳答案
有点像
re.findall (r'(?<=moving towards )[^-.]*', string)
['a particular point']
(?<=moving towards )
回顾断言断言字符串前面有moving towards
[^-.]*
匹配除a-
或.
之外的任何内容如何匹配
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