如何设计正则表达式模式以匹配字符串

时间:2014-08-22 11:41:07

标签: python regex

import re
text = "**Company** [Click1](call:58200) ******provides*** **housekeeping** **clean***"
bold = re.findall(r'\*{2}(\w+?)\*{2}', text)
print bold
  

结果: ['公司','提供','管家''清洁']   
期待: [' **公司**',' **管家**']

如何设计正则表达式模式以进行grep?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用Lookaround

  

环视实际匹配字符,但随后放弃匹配,仅返回结果:匹配或不匹配

(?<!\*)\*{2}\w+?\*{2}(?!\*)

Online demo

示例代码:

import re
p = re.compile(ur'(?<!\*)\*{2}\w+?\*{2}(?!\*)')
test_str = u"**Company** [Click1](call:58200) ******provides*** **housekeeping** **clean***"

re.findall(p, test_str)

模式说明:

  (?<!                     look behind to see if there is not: 
    \*                       '*'
  )                        end of look-behind

   \*{2}                    '*' (2 times)
  \w+?                     word characters (a-z, A-Z, 0-9, _) (1 or more times)
   \*{2}                    '*' (2 times)

   (?!                      look ahead to see if there is not:
     \*                       '*'
   )                        end of look-ahead