python - How to find characters not in parentheses -
attempting find occurrences of characters
string1 = '%(example_1).40s-%(example-2)_-%(example3)s_' so output has occurrences of '-' '_' not in parentheses
['-', '_', '-', '_'] do not need care nested parentheses
the following give output.:
>>> import re >>> str = '%(example_1).40s-%(example-2)_-%(example3)s_' >>> print list("".join(re.findall("[-_]+(?![^(]*\))", str))) ['-', '_', '-', '_'] what finds substrings containing '-' and/or '_' in str , not in parentheses. since these substrings, such matching characters joining, , splitting list.
Comments
Post a Comment