我在Python中进行正则表达式匹配.我试图遵循一些组合,但没有工作.我是正则表达式的新手.我的问题是,我有一个字符串如下.
string = ''' World is moving towards a particular point'''
Run Code Online (Sandbox Code Playgroud)
我想要一个解决方案来检查单词"moving"之后是否存在单词"towards",如果是,我想选择该行的其余部分(在'towards'之后),直到它以'.'结尾.或' - '.我是新手.请提供一些好的建议.
就像是
re.findall (r'(?<=moving towards )[^-.]*', string)
['a particular point']
Run Code Online (Sandbox Code Playgroud)
(?<=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
|
[^-.]
Run Code Online (Sandbox Code Playgroud)