In Python, the (?P<group_name>…) syntax allows one to refer to the matched string through its name:
>>> import re
>>> match = re.search('(?P<name>.*) (?P<phone>.*)', 'John 123456')
>>> match.group('name')
'John'
What does “P” stand for? I could not find any hint in the official documentation.
I would love to get ideas about how to help my students remember this syntax. Knowing what “P” does stand for (or might stand for) would be useful.
Since we’re all guessing, I might as well give mine: I’ve always thought it stood for Python. That may sound pretty stupid — what, P for Python?! — but in my defense, I vaguely remembered this thread [emphasis mine]:
to which Larry Wall replied:
So I don’t know what the original choice of P was motivated by — pattern? placeholder? penguins? — but you can understand why I’ve always associated it with Python. Which considering that (1) I don’t like regular expressions and avoid them wherever possible, and (2) this thread happened fifteen years ago, is kind of odd.