pythonregexvariablesoptional-parametersoptional-variables

Regular expression strings with consecutive variables


I'm fairly new to regex (regular expressions) and need a little bit of help formulating a string. I understand it for the most part but got stumped when the text I needed to match had variables followed by an optional phrase.

Say the text is formatted something like "turn $1 [the] lights" where "$1" is the variable I want while "the" can be included or left out. I've tried the following blurb, "turn (.+) (?:the)?\s*lights", which works for "turn on lights":

>>> re.match("turn (.+) (?:the)?\s*lights", "turn on lights").groups()
("on",)

But when I include the "the" and try to match "turn on the lights", I get "on the" as my variable.

>>> re.match("turn (.+) (?:the)?\s*lights", "turn on the lights").groups()
("on the",)

Is this something that can be accomplished with the regex library? I apologize if the question is unclear, thank you in advance!


Solution

  • You just need to use a lazy quantifier for this:

    turn (.+?) (?:the)?\s*lights
    

    RegEx Demo