Tokenize a string with escaping: Difference between revisions

m
→‎Regex-based: ordering of regexes is not important (at least in this case)
(→‎{{header|Python}}: add regex-based version)
m (→‎Regex-based: ordering of regexes is not important (at least in this case))
Line 2,705:
The python <code>re</code> library has a handy class <code>Scanner</code> which is intended precisely for this use-case.
It takes a list of pairs '''regex, action''' and whenever it encounters '''regex''' in the input, it executes '''action'''.
This allows us to solve this task very efficiently with minimum effort, the hardest part being the correct definition and ordering of the regular expressions.
 
The following code also illustrates an important feature of Python ‒ nested functions with closures.