Re: [Q] How to search the back slash?

Doug Wyatt (wyatt@rahul.net)
29 Jan 1995 02:12:06 GMT

The problem is that the regular expression parser _also_ treats backslash
as a metacharacter. To include a literal backslash in a regular expression,
you need two backslashes; and to type a string literal with two backslashes,
you need four! (See the paragraph beginning "Please note:" in section 3.7
of the Python Library Reference.)

>>> pat = '\\\\'
>>> print pat
\\
>>> prog = regex.compile(pat)
>>> s = 'ab\\cd'
>>> print s
ab\cd
>>> prog.search(s)
2

P.S. It would be truly wonderful if that error message really _were_
"regex.error: Regular expression ends prematurel"!

-- 
Doug Wyatt | wyatt@rahul.net | <URL:http://www.rahul.net/wyatt/>