我有以下字符串:正則表達式的findall檢索基礎上開始和結束字符子串
6[Sup. 1e+02]
我試圖找回的只是1e+02
一子。該變量首先引用上面指定的字符串。以下是我所嘗試過的。
re.findall(' \d*]', first)
我有以下字符串:正則表達式的findall檢索基礎上開始和結束字符子串
6[Sup. 1e+02]
我試圖找回的只是1e+02
一子。該變量首先引用上面指定的字符串。以下是我所嘗試過的。
re.findall(' \d*]', first)
您需要使用下面的正則表達式:
\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)
import re
first = "6[Sup. 1e+02]"
result = re.findall(r"\s+(.*?)\]", first)
print result
輸出:
['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 «\]»
中的其他位置存在干擾空白時如果輸入字符串中包含更多沒有數字的'[]'s? '6 [增刊1e + 02] [lorem ipsum]'? http://ideone.com/mh233v –
OP說:「__我有以下字符串:__'6 [Sup。1e + 02]'」 –
我一直希望有更好的東西,但它的工作原理 – BadProgrammer
@BillGates:它可以是整潔的,但你只提供了一個例子。我猜所有這些值都在方括號內,它們位於結束之前。如果不是,請嘗試'\ b \ d + e \ + \ d + \ b'。 –
'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瞭解更多詳情。 –