Regex findall to get substring based on start and end character
2 answers
You need to use the following regex:
\b\d+e\+\d+\b
Explanation:
-
\b
- word boundary -
\d+
- numbers, 1 or more -
e
- Literale
-
\+
- Literal+
-
\d+
- numbers, 1 or more -
\b
- word boundary
Watch the 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
+2
source to share
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 «\]»
+1
source to share