I’m trying to match rc-update -s output in python.
m = re.match(r"^\s*(\w+)\s*\|{\s*(\w+)\s*}*$", " network | level1 level2 leveln ")
but m is always None
the hard part for me is getting the regex to match the n levels. I thought that using {}* would match the n levels, but as soon as I add the {} nothing matches.
thanks.
The curly braces (“{}”) do not do what you think they do, at least in this example.
You seem to want a non-matching group. With Python’s
re, the syntax for this is(?:\s*(\w+)\s*), to match your example.With this change to your example, I get:
Note that the result only contains the last match for the repeated group. If you want to get all of the matches, match the entire expression containing the repetitions, and then parse that to find each of the matches. For example:
On a side note, this looks like something that would be much simpler to parse without regexps. As you can see, regexps have a lot of gotchas and become confusing very quickly.