Home > Back-end >  regex groups with uneven number of groups
regex groups with uneven number of groups

Time:06-18

I'm not good with terms used in regex, so picking a suitable title for this question is some how difficult for me, so feel free the suggest a good title.

but anyway I have this txt and regex expression

import re
txt = """
%power {s}
shop %power {w}
%electricity {t}
""".replace('\n',' ')
x = re.findall("((\%?\w \s)*)\{([^\}]*)\}",txt)

the result is [('%power ', '%power ', 's'), ('shop %power ', '%power ', 'w'), ('%electricity ', '%electricity ', 't')] but I was intended to get [('%power ', 's'), ('shop ', '%power ', 'w'), ('%electricity ', 't')] so how can I achieve the desired?

CodePudding user response:

You need to pip install regex and then use

import regex
txt = """
%power {s}
shop %power ow {w}
%electricity {t}
""".replace('\n',' ')
x = regex.finditer(r"(%?\w \s)*\{([^{}]*)}", txt)
z = [tuple(y.captures(1)   y.captures(2)) for y in x] 
print(z)

See the Python demo.

Output:

[('%power ', 's'), ('shop ', '%power ', 'ow ', 'w'), ('%electricity ', 't')]
  • Related