473,395 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trouble with regexes

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'

Jul 19 '05 #1
2 1055
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>

Jul 19 '05 #2
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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
2
by: James | last post by:
Hi I am having some trouble getting a asp page to load. Im a noob to the asp side. I have followed knowledege base Article 301305. I am running 2000 adv, IIS 5.0 I have the following...
4
by: Jacek Dziedzic | last post by:
Hi! First of all, I hope my problem is not too loosely tied to the "standard C++" that is the topic of this group. I have some code that exhibits a strange behaviour: on one computer, where I...
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
1
by: Jim Bancroft | last post by:
Hi everyone, I'm running into a problem with my ASP.Net application. I've just created a new aspx page which uses some new components of mine that inherit from ServicedComponent and are...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
2
by: JLupear | last post by:
I am having trouble with my code again, I had prepared a question and the code to upload, however I am having trouble posting it, are there limits to the amount of lines you can post? I split it...
4
by: tinkerbarbet | last post by:
Hi I've read around quite a bit about Unicode and python's support for it, and I'm still unclear about how it all fits together in certain scenarios. Can anyone help clarify? * When I say "#...
0
by: mrchatgroup | last post by:
news from http://www.mrchat.net/myblog/myblog/small-accidents-mean-big-trouble-for-supercollider.html Small Accidents Mean Big Trouble for Supercollider Image Scientists expect startup...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.