正则表达式 findall 以根据开始和结束字符检索子字符串



>我有以下字符串:

6[Sup. 1e+02]

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

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

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

bd+e+d+b

解释

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

查看演示

示例代码:

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

查看 IDEONE 演示

import re
first = "6[Sup. 1e+02]"
result = re.findall(r"s+(.*?)]", first)
print result

输出:

['1e+02']

演示http://ideone.com/Kevtje


正则表达式解释:

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 «]»

最新更新