将“:”与正则表达式匹配

时间:2021-05-07 09:44:52

标签: python regex

在这里,我想在文本中找到一个句子,它以单词 expert 开头,并且 in 直到 (点): >

import re

string = 'We need a person expert in python. also familiar with django.'
pattern = re.finditer(r'(expert)[^.]*\b(in)\b[^.]*[.]', string)
for p in pattern:
    print(p.group(0))
    
# output:expert in python.

除了 .(点)我还想添加 ":"(引号冒号引号)(我知道我们不能使用 ( ) 在 [] 中,但仅用于澄清问题):

pattern = re.finditer(r'(expert)[^.(":")]*\b(in)\b[^.(":")]*[.(":")]', string)

所以对于 string = We need a person expert in python":" also familiar in django. print(p.group(0)) 必须给 expert in python":"

1 个答案:

答案 0 :(得分:2)

在先匹配 . 之前,您可以使用模式不交叉匹配 ":"inin

然后匹配直到第一次出现 .":"

\bexpert(?:(?!":"|\.|\bin\b).)*\bin\b.*?(?:\.|":")
  • \bexpert 一个词边界,匹配 expert
  • (?:(?!":"|\.|\bin\b).)* 匹配 0+ 次任何字符,除了断言直接在右侧的内容不是 ":". 或单词 in
  • \bin\b 在单词边界之间匹配 in 以防止部分匹配
  • .*?(?:\.|":") 匹配尽可能少的字符,直到第一次出现 .":"

Regex demo