Trouble with regexes

F

Fernando Rodriguez

Hi,

I'm trying to write a regex that matches a \r char if and only if it
is not followed by a \n (I want to translate text files from unix
newlines to windows\dos).

I tried this, but it doesn't work:
p = re.compile(r'(\r)[^\n]', re.IGNORECASE)

it still matches a string such as r'\r\n'
 
F

Fredrik Lundh

Fernando said:
I'm trying to write a regex that matches a \r char if and only if it
is not followed by a \n (I want to translate text files from unix
newlines to windows\dos).

Unix uses \n and Windows uses \r\n, so matching lone \r isn't
going to help you the slighest... (read on)
I tried this, but it doesn't work:
p = re.compile(r'(\r)[^\n]', re.IGNORECASE)

it still matches a string such as r'\r\n'
really?
import re
p = re.compile(r'(\r)[^\n]', re.IGNORECASE)
print p.match('\r\n') None
print p.match(r'\r\n')
None

on the other hand,


it might be a good idea to play a little more with ''-literals and r''-
literals (and print x and print repr(x)) until you understand exactly
how things work...

:::
I want to translate text files from unix newlines to windows\dos

you don't need regular expressions for that; the easiest way to
convert any kind of line endings to the local format is to open the
source file with the "U" flag:

infile = open(filename, "rU") # universal line endings
outfile = open(outfilename, "w") # text mode is default

s = infile.readline()
outfile.write(s)

:::

if you're converting files from Unix format to Windows format on a
Windows box, you don't have to do anything -- just open the files
in text mode, and Python's file I/O layer will fix the rest for you.

</F>
 
T

Tim Roberts

Fernando Rodriguez said:
I'm trying to write a regex that matches a \r char if and only if it
is not followed by a \n (I want to translate text files from unix
newlines to windows\dos).

I tried this, but it doesn't work:
p = re.compile(r'(\r)[^\n]', re.IGNORECASE)

it still matches a string such as r'\r\n'

Hint: the string r'\r\n' contains four characters. It contains neither
carriage return nor newline.

Bigger hint: the string '\r\n' contains two characters.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top