Omar Ahmed
Omar Ahmed

Reputation: 159

Writing a function to extract integers from strings using regular expressions

Trying to Write function integers_in_brackets that finds from a given string all integers that are enclosed in brackets.

Example run: integers_in_brackets(" afd [asd] [12 ] [a34] [ -43 ]tt [+12]xxx") returns [12, -43, 12]. So there can be whitespace between the number and the brackets, but no other character besides those that make up the integer.

My progress so far was:

def integers_in_brackets(s):
    r= []
    patt = re.compile(r'\W\s*(-?\d+)')
    for i in patt.findall(s):
        r.append(int(i))
    return r

Yet i seem to fail in TMC where I only achieve 66% of the required

Failed: test.test_integers_in_brackets.IntegersInBrackets.test_second
        Lists differ: [128, 47, -43, 12] != [47, 12]

First differing element 0:
128
47

First list contains 2 additional elements.
First extra element 2:
-43

- [128, 47, -43, 12]
+ [47, 12] : Incorrect result for string   afd [128+] [47 ] [a34]  [ +-43 ]tt [+12]xxx!

Test results: 2/3 tests passed
 66%[????????????????????????????????????????????????????????????????]

Upvotes: 0

Views: 456

Answers (3)

Random Forest
Random Forest

Reputation: 11

Easy and more straightforward way to do it:

re.findall(r'\[\s*([+-]?\d+)\s*\]',"  afd [asd] [12 ] [a34]  [ -43 ]tt [+12]")

Upvotes: 1

oppressionslayer
oppressionslayer

Reputation: 7214

This should work:

import re
pat=r"(?:\[(\s*?[-+]?\d+\s*?)\])"
list(map(eval, re.findall(pat, "  afd [asd] [12 ] [a34]  [ -43 ]tt [+12]")))                                                                                                        
#[12, -43, 12]

Upvotes: 2

Anonymous
Anonymous

Reputation: 12017

Explicit brackets should work:

def integers_in_brackets(s):
    r= []
    patt = re.compile(r'\[\s*(\+?-?\d+)\s*\]')
    for i in patt.findall(s):
        r.append(int(i))
    return r

Upvotes: 1

Related Questions