3

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)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
BadProgrammer
  • 371
  • 3
  • 17

2 Answers2

2

You need to use the following regex:

\b\d+e\+\d+\b

Explanation:

  • \b - Word boundary
  • \d+ - Digits, 1 or more
  • e - Literal e
  • \+ - Literal +
  • \d+ - Digits, 1 or more
  • \b - Word boundary

See 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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • @BillGates: It can be neater, but you only provided one example. I guess all these values are inside square brackets, and they are located right before the closing one. If not, try just `\b\d+e\+\d+\b`. – Wiktor Stribiżew Apr 28 '15 at 21:10
  • `r` - raw string, where we do not have to use double slashes when building regexes, `u` means a Unicode string. Please check http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-in-python-and-what-are-raw-string-l for more details. – Wiktor Stribiżew Apr 28 '15 at 21:17
1
import re
first = "6[Sup. 1e+02]"
result = re.findall(r"\s+(.*?)\]", first)
print result

Output:

['1e+02']

Demo http://ideone.com/Kevtje


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 «\]»
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268