Why do we use question mark literal in Python regular expression?

PythonServer Side ProgrammingProgramming

The question mark literal is used in different ways in Python Regular Expressions

Special character '?'

As a special character '?' causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either ‘a’ or ‘ab’

The question mark literal '?' is used following ways as per Python docs.

Making the quantifier non-greedy

*?, +?, ??

The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.*> is matched against '<a> b <c>', it will match the entire string, and not just '<a>'. Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using the RE <.*?> will match only '<a>'.

Published on 11-Jan-2018 11:36:53