I have the following string:
6[Sup. 1e+02]
I'm trying to retrieve a substring of just 1e+02
. The variable first refers to the above specified string. Below is what I have tried.
re.findall(' \d*]', first)
I have the following string:
6[Sup. 1e+02]
I'm trying to retrieve a substring of just 1e+02
. The variable first refers to the above specified string. Below is what I have tried.
re.findall(' \d*]', first)
You need to use the following regex:
\b\d+e\+\d+\b
Explanation:
\b
- Word boundary\d+
- Digits, 1 or moree
- Literal e
\+
- Literal +
\d+
- Digits, 1 or more\b
- Word boundarySee demo
Sample code:
import re
p = re.compile(ur'\b\d+e\+\d+\b')
test_str = u"6[Sup. 1e+02]"
re.findall(p, test_str)
See IDEONE demo
import re
first = "6[Sup. 1e+02]"
result = re.findall(r"\s+(.*?)\]", first)
print result
Output:
['1e+02']
regex Explanation:
\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 «\]»