[Solved] Extract all numbers in brackets with Python [closed]


Regex can do that:

s = "asd[123]348dsdk[45]sdhj71[6789]sdfh"
import re
s_filter=" ".join(re.findall(r"\[(\d+)\]",s)))

print(s_filter)

Output:

123 45 6789

Pattern explained:

\[         \]   are the literal square brackets
    (\d+?)      as as few numbers inside them as capture group

re.findall finds them all and ' '.join(iterable)combines them back into a string.

2

solved Extract all numbers in brackets with Python [closed]