list of regex special characters

Tim Chase python.list at tim.thechases.com
Sun Nov 28 19:23:46 EST 2010


On 11/28/2010 05:58 PM, goldtech wrote:
> I am looking for a list of special character in python regular
> expressions that need to be escaped if you want their literal meaning.
>
> I searched and can not find the list. Any help appreciated.

Trust the re module to tell you:

  >>> import re
  >>> chars = [chr(i) for i in range(0,256)]
  >>> escaped = [c for c in chars if re.escape(c) != c]
  >>> print len(escaped)
  194
  >>> print escaped
  [...]
  >>> can_use_unescaped = [c for c in chars if re.escape(c) == c]

(adjust "chars" accordingly if you want to check unicode 
characters too).

-tkc






More information about the Python-list mailing list