Attempting to find all occurrences of characters
str = '%(example_1).40s-%(example-2)_-%(example3)s_'
['-', '_', '-', '_']
The following will give you your output.:
>>> import re >>> str = '%(example_1).40s-%(example-2)_-%(example3)s_' >>> print list("".join(re.findall("[-_]+(?![^(]*\))", str))) ['-', '_', '-', '_']
What this does is it finds all the substrings containing
str and not in parentheses. Since these are substrings, we get all such matching characters by joining, and splitting into a list.