How to escape # hash character in regex match strings

Tim Chase python.list at tim.thechases.com
Thu Jun 11 18:19:33 EDT 2009


>> I'm surprised it's been so difficult to find an example of the hash
>> character in a RegEx string -- for exactly this type of situation,
>> since it's so common in the real world that people want to put a pound
>> symbol in front of a number.
> 
> It's a character with no special meaning to the regex engine, so I'm not
> in the least surprised that there aren't many examples containing it.
> You could just as validly claim that there aren't many examples involving
> the letter 'q'.

It depends on whether the re.VERBOSE option is passed.  If you're using 
a verbose regexp, you can use "#" to comment portions of it:

   r = re.compile(r"""
      \d+   # some digits
      [aeiou] # some vowels
      """, re.VERBOSE)

-tkc






More information about the Python-list mailing list