Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Trouble with regexes

Reply
Thread Tools

Trouble with regexes

 
 
Fernando Rodriguez
Guest
Posts: n/a
 
      05-25-2005
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'

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      05-25-2005
Fernando Rodriguez wrote:

> 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,

<_sre.SRE_Match object at 0x0083B160>
>>> print p.match('\rx')

<_sre.SRE_Match object at 0x0083B120>
>>> print p.match(r'\rx')


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>



 
Reply With Quote
 
 
 
 
Tim Roberts
Guest
Posts: n/a
 
      05-27-2005
Fernando Rodriguez <frr@THOU_SHALL_NOT_SPAMeasyjob.net> wrote:
>
>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.
--
- Tim Roberts, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem extracting data from clipboard using java regexes spam.eggs.bacon@gmail.com Java 0 09-29-2006 08:17 AM
MoinMoin WikiName and python regexes Ara.T.Howard Python 6 06-26-2005 05:47 PM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Does Python optimize regexes? Jason Smith Python 5 07-03-2004 02:36 PM
File.separatorChar and regexes. Roedy Green Java 0 08-22-2003 02:43 AM



Advertisments