regex findall基于开始和结束字符检索子字符串

时间:2015-04-28 21:00:47

标签: regex python-2.7

我有以下字符串:

6[Sup. 1e+02]

我正在尝试检索仅1e+02的子字符串。变量首先引用上面指定的字符串。以下是我的尝试。

re.findall(' \d*]', first)

2 个答案:

答案 0 :(得分:2)

您需要使用以下正则表达式:

\b\d+e\+\d+\b

解释

  • \b - 字边界
  • \d+ - 数字,1个或更多
  • e - 文字e
  • \+ - 文字+
  • \d+ - 数字,1个或更多
  • \b - 字边界

请参阅demo

示例代码:

import re
p = re.compile(ur'\b\d+e\+\d+\b')
test_str = u"6[Sup. 1e+02]"
re.findall(p, test_str)

请参阅IDEONE demo

答案 1 :(得分:1)

['1e+02']

输出:

\s+(.*?)\]

Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) «\s+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regex below and capture its match into backreference number 1 «(.*?)»
   Match any single character that is NOT a line break character (line feed) «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “]” literally «\]»

演示 http://ideone.com/Kevtje

正则表达式解释:

{{1}}