正则表达式仅与特定条件匹配

时间:2016-06-17 23:08:06

标签: regex python-2.7

1 Abc凹痕意味着账单并享受磨坊

2 Abc凹痕意味着(abc凹痕意味着票据和享受磨坊)

我想使用常规的rexpression获取这个词,意思是' 只有当它介于'之间时才意味着'和'和'。

所以上面的答案应该是

比尔

比尔

我怎样才能在Python中使用正则表达式 我在stackoverflow上搜索它但无法找到答案

2 个答案:

答案 0 :(得分:1)

描述

Python允许使用可以验证所需子字符串的外观,而无需实际捕获它。

(?<=\bmeant\s)\w+(?=\sand\b)

Regular expression visualization

此正则表达式将执行以下操作:

  • meantand
  • 之间找到单个字词
  • 要求meantand是独立的单词,而不是嵌入其他单词。

实施例

现场演示

https://regex101.com/r/eE8hF2/3

示例文字

请注意第3行中的边框大小写,其中单词android以单词and开头,因此可以避免匹配。

1 Abc dent meant bill and enjoy mill

2 Abc dent meant ( abc dent meant droids and enjoy mill )

3 Arthur Dent meant tractors android when he said droid.

样本匹配

MATCH 1
1.  [17-21] `bill`

MATCH 2
1.  [72-78] `droids`

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  (?<=                     look behind to see if there is:
----------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
----------------------------------------------------------------------
    meant                    'meant'
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  )                        end of look-behind
----------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
    and                      'and'
----------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------

答案 1 :(得分:0)

meant\s+([a-z]+)\s+and

[a-z] +多次匹配a和z之间的所有字符

\ s +匹配多个空格

围绕[a-z] +的括号将匹配放入捕获组,以便以后可以使用

这里有效:https://regex101.com/r/hW5fP7/3

这是一个很好的网站,可以了解有关正则表达式的更多信息:http://www.regular-expressions.info/tutorial.html