Re: EOL - scanning single-quoted string


A

Ajay

hi!

i got the escape character bit, but i still get an errorTraceback (most recent call last):
File "<stdin>", line 1, in ?
File "c:\Python23\lib\sre.py", line 151, in subn
return _compile(pattern, 0).subn(repl, string, count)
File "c:\Python23\lib\sre.py", line 258, in _subx
template = _compile_repl(template, pattern)
File "c:\Python23\lib\sre.py", line 245, in _compile_repl
raise error, v # invalid expression
sre_constants.error: bogus escape (end of line)

any ideas?

cheers

--
Ajay Brar,
CS Honours 2004
Smart Internet Technology Research Group


Quoting Gandalf said:
Hi Ajay!

The problem is that you must represent the backslash as '\\'. This is
because a simple backslash opens an escape sequence.
For example:

'\n' - this is new line
'\t' - this is TAB
'\\' - this is the backslash itself

You should use this:

re.subn('/', '\\', str)

The other way is to use raw strings. You can read more about these
things here:

http://docs.python.org/ref/strings.html

Best,

Laci 2.0
 
Ad

Advertisements

C

Christopher T King

i got the escape character bit, but i still get an error

Ah... regex quoting issues. The '\\' makes a single '\' appear in the
string, but being a regex, a single '\' in a string is again interpreted
as a backslash. You will either have to write '\\\\' or use a raw string,
r'\\'. Raw strings pass backslashes untouched, so you only need to
backslash it once for the regex.

However, the best solution for your problem is not to use regexes, but to
use the os.path module:
'C:\\My Documents\\um_ajay.xml'

normpath() 'tidies up' the path, a task which includes correcting path
seperators, amoung other things. abspath() does the same thing as
normpath(), but also resolves relative paths into absolute ones. Always
look to os.path when doing anything involving paths; it probably has the
function you want, and is portable across platforms.
 
Ad

Advertisements

A

Ajay Brar

Christopher said:
Ah... regex quoting issues. The '\\' makes a single '\' appear in the
string, but being a regex, a single '\' in a string is again interpreted
as a backslash. You will either have to write '\\\\' or use a raw string,
r'\\'. Raw strings pass backslashes untouched, so you only need to
backslash it once for the regex.

However, the best solution for your problem is not to use regexes, but to
use the os.path module:



'\\My Documents\\um_ajay.xml'


'C:\\My Documents\\um_ajay.xml'

normpath() 'tidies up' the path, a task which includes correcting path
seperators, amoung other things. abspath() does the same thing as
normpath(), but also resolves relative paths into absolute ones. Always
look to os.path when doing anything involving paths; it probably has the
function you want, and is portable across platforms.
thanks. works well now


--
Ajay Brar
CS Honours 2004
Smart Internet Technology Research Group

http://www.it.usyd.edu.au/~abrar1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top