2015-04-28 22 views

回答

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

+0

我一直希望有更好的東西,但它的工作原理 – BadProgrammer

+0

@BillGates:它可以是整潔的,但你只提供了一個例子。我猜所有這些值都在方括號內,它們位於結束之前。如果不是,請嘗試'\ b \ d + e \ + \ d + \ b'。 –

+0

'r' - 原始字符串,在構建正則表達式時我們不必使用雙斜線,'u'表示Unicode字符串。請查看http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-in-python-and-what-are-raw-string-l瞭解更多詳情。 –

1
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 «\]» 
+0

中的其他位置存在干擾空白時如果輸入字符串中包含更多沒有數字的'[]'s? '6 [增刊1e + 02] [lorem ipsum]'? http://ideone.com/mh233v –

+0

OP說:「__我有以下字符串:__'6 [Sup。1e + 02]'」 –

相關問題