Regex findall to get substring based on start and end character

I have the following line:

6[Sup. 1e+02]


I am trying to get the substring only 1e+02

. First, the variable refers to the above line. Below I have tried.

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



source to share

2 answers

You need to use the following regex:




  • \b

    - word boundary
  • \d+

    - numbers, 1 or more
  • e

    - Literal e

  • \+

    - 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



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






regex Explanation:


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




All Articles