正则表达式匹配一个特定的单词后跟另一个单词,以及一个边界标识符
Regex to match a particular word followed by another, along with a boundary identifier
我正在 Python 中进行正则表达式匹配。我尝试遵循一些组合但没有用。我对正则表达式完全陌生。我的问题是,我的字符串如下。
string = ''' World is moving towards a particular point'''
我想要一个解决方案来检查单词 "towards" 是否紧跟在单词 "moving" 之后,如果是,我想 select 该行的其余部分(在 'towards' ) 直到它以 '.' 结尾或“-”。我是新手。请提供一些好的建议。
您需要使用 negative-look around 。但是当你的字符串中有 .
或 -
时它会起作用,如果你没有,你可以使用 @nu11p01n73R 的答案。 :
>>> string = ''' World is moving towards a particular point.'''
>>> re.search(r'(?<=moving towards).*(?=\.|-)',string).group(0)
' a particular point'
(?<=moving towards).*
为负数look-behind匹配moving towards
后的所有(.*)
和 (?=\.|-)'
是负数 look-behind 匹配所有之前 (\.|-
) 这意味着 .
或 -
类似
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
|
[^-.]
import re
str = ''' World is moving towards a particular point'''
match = re.search('moving towards\s+([^.-]+)', str)
if match:
var = match.group(1)
Output >>> a particular point
正则表达式调试link
https://www.regex101.com/r/wV7iC7/1
要重复这个问题,您要检查给定的 string
中单词 "moving" 是否后跟 "towards"。这是使用 parse 模块执行此操作的方法:
import parse
string = "World is moving towards a particular point"
fmt = "moving {:w}"
result = parse.search(fmt, string)
assert result[0] == "towards"
请注意,:w
格式规范会导致结果匹配字母和下划线。
我正在 Python 中进行正则表达式匹配。我尝试遵循一些组合但没有用。我对正则表达式完全陌生。我的问题是,我的字符串如下。
string = ''' World is moving towards a particular point'''
我想要一个解决方案来检查单词 "towards" 是否紧跟在单词 "moving" 之后,如果是,我想 select 该行的其余部分(在 'towards' ) 直到它以 '.' 结尾或“-”。我是新手。请提供一些好的建议。
您需要使用 negative-look around 。但是当你的字符串中有 .
或 -
时它会起作用,如果你没有,你可以使用 @nu11p01n73R 的答案。 :
>>> string = ''' World is moving towards a particular point.'''
>>> re.search(r'(?<=moving towards).*(?=\.|-)',string).group(0)
' a particular point'
(?<=moving towards).*
为负数look-behind匹配moving towards
和 (?=\.|-)'
是负数 look-behind 匹配所有之前 (\.|-
) 这意味着 .
或 -
类似
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
|
[^-.]
import re
str = ''' World is moving towards a particular point'''
match = re.search('moving towards\s+([^.-]+)', str)
if match:
var = match.group(1)
Output >>> a particular point
正则表达式调试link
https://www.regex101.com/r/wV7iC7/1
要重复这个问题,您要检查给定的 string
中单词 "moving" 是否后跟 "towards"。这是使用 parse 模块执行此操作的方法:
import parse
string = "World is moving towards a particular point"
fmt = "moving {:w}"
result = parse.search(fmt, string)
assert result[0] == "towards"
请注意,:w
格式规范会导致结果匹配字母和下划线。