My Colleague With the Super Power!

I once had a colleague with a super power.

No, he couldn’t fly like Superman, manipulate minds like Professor Xavier or heal like Wolverine. He had a really useful super power: He could read Python regular expressions!

For mere mortals regular expressions are write-only collections of control characters and they have a very hard time understanding, let alone analyzing or debugging them. For those of us who don’t have a super hero ready to rescue us damsels in distress, we can use commented regular expressions, as in

1
2
3
>>> re.search(r"""foo # Find the marker
                  . # And a random separator
                  (\w\w\w) # We want the three-character group after the separator""", 'Get results for foo:bar', re.X).groups()[0]

This is the same as

1
>>> re.search(r"""foo.(\w\w\w)""", 'Get results for foo:bar').groups()[0]

Except you’ll have a chance to understand the former, but probably not the latter.