简体   繁体   English

Python 正则表达式前瞻断言

[英]Python regex lookahead assertion

I want to get word1 and word2 from text string like text = "otherstuff word1 + word2" .我想从text = "otherstuff word1 + word2"这样的文本字符串中获取 word1 和 word2。

I managed to get one word using this lookbehind regular expression:我设法使用这个后向正则表达式得到一个词:

r1 = re.search(r'(?<=\+\s)\w+', text) # Get word2

But whenever I try to do the same with the lookahead, regex it doesn't return anything但是每当我尝试对前瞻做同样的事情时,正则表达式它不会返回任何东西

r2 = re.search(r'(?=(\s\+))\w+', text) # Get None

What did I do wrong?我做错了什么?

Could you try this?你能试试这个吗? Just swapped position of your \w+ and (?=(\s\+)) .刚刚交换了您的\w+(?=(\s\+)) position 。


r2 = re.search(r'\w+(?=(\s\+))', text)

What did I do wrong?我做错了什么?

This pattern (?=(\s\+))\w+ asserts what is directly to the right from the current position should be a whitespace char \s but then you start the match with a word char \w which will not match.此模式(?=(\s\+))\w+断言从当前 position 直接向右的内容应该是一个空白字符\s但然后你以一个不匹配的单词 char \w开始匹配。

You could get both words by using a single pattern with an alternation |您可以通过使用带有交替的单个模式来获得这两个词| and switch the order of the lookaround.并切换环视的顺序。

(?<=\+\s)\w+|\w+(?=\s\+)

Regex demo正则表达式演示


To get both words, you could also use 2 capturing groups instead of lookarounds:要获得这两个词,您还可以使用 2 个捕获组而不是环视:

(\w+)\s\+\s(\w+)

Regex demo正则表达式演示

import re

pattern = r"(\w+)\s\+\s(\w+)"
s = "otherstuff word1 + word2."
print(re.findall(pattern, s))

Output Output

[('word1', 'word2')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM