list of regex special characters

Ben Finney ben+python at benfinney.id.au
Sun Nov 28 19:49:27 EST 2010


Tim Chase <python.list at tim.thechases.com> writes:

> 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.
>
> 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]

Note that, according to its docstring, ‘re.escape’ doesn't distinguish
characters that *need to be* escaped for their literal meaning; it
simply escapes any non-alphanumeric character.

>  >>> can_use_unescaped = [c for c in chars if re.escape(c) == c]

Right. There are three classes of character for this purpose:

* those that have a literal meaning *only if* escaped
* those that have literal meaning whether or not they are escaped
* those that have a literal meaning *only if not* escaped

The ‘re.escape’ function, according to its docstring, simply says any
non-alphanumerics can safely be said to exist in one of the first two
classes, and both are safe to escape without bothering to distinguish
between them.

The OP was asking for the first class specifically, but I question
whether that's actually needed for the purpose.

-- 
 \           “The cost of education is trivial compared to the cost of |
  `\                                     ignorance.” —Thomas Jefferson |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list