Reputation: 23
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
Reputation: 163362
Another option to match the values might be shortening the pattern and use re.findall
\[\s*\+?(-?\d+)\s*\]
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
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