UAT-PyFo
UAT-PyFo

Reputation: 23

Extracting the integers enclosed in square brackets

I am trying to extract the integers out of a string using regular expressions in Python. So, there can be whitespace between the number and the brackets, but no other character besides those that make up the integer. My code looks like:


import re
s = "  afd [asd] [12 ] [a34]  [ -43 ]tt [+12]xxx"
print(re.findall(r"\s+\[\w+\]\s\[(\d+)\s\]\s\[\w+\]\s+\[\s(-\d+)\s\]\w+\s\[\+(\d+)\]\w+",s))

The above code prints:


[('12', '-43', '12')]

However, I want the output not to be in the form of tuple, or triple in the above case, but in the list like:


[12, -43, 12]

Upvotes: 0

Views: 266

Answers (2)

The fourth bird
The fourth bird

Reputation: 163362

Another option to match the values might be shortening the pattern and use re.findall

\[\s*\+?(-?\d+)\s*\]

Regex demo | Python demo

For example

import re

regex = r"\[\s*\+?(-?\d+)\s*\]"
test_str = "afd [asd] [12 ] [a34]  [ -43 ]tt [+12]xxx"

print(re.findall(regex, test_str))

Output

['12', '-43', '12']

Upvotes: 0

CDJB
CDJB

Reputation: 14516

You can do this with the following:

import re
s = "  afd [asd] [12 ] [a34]  [ -43 ]tt [+12]xxx"
rv = re.findall(r"\s+\[\w+\]\s\[(\d+)\s\]\s\[\w+\]\s+\[\s(-\d+)\s\]\w+\s\[\+(\d+)\]\w+",s)
lst = [int(x) for t in rv for x in t]

This will loop over all tuples in rv and add each integer to a new list, lst. The output in this case is:

>>> lst
[12, -43, 12]

Upvotes: 1

Related Questions