473,396 Members | 1,775 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,396 software developers and data experts.

[regex] Basic rewriting

Hello

I've been reading tutorials on regexes in Python, but I still
don't get it:

========
#!/usr/bin/python

#myscript.py 0123456789

import sys,re

#Turn 0123456789 into 01.23.45.67.89
p = re.compile('(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)')
phone = p.sub('\1.\2.\3.\4.\5',sys.argv[1])
print phone
========

=Python displays "...." instead. Any idea what I'm doing wrong?

Thank you.
Nov 21 '07 #1
3 964
I've been reading tutorials on regexes in Python, but I still
don't get it:

========
#!/usr/bin/python

#myscript.py 0123456789

import sys,re

#Turn 0123456789 into 01.23.45.67.89
p = re.compile('(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)')
phone = p.sub('\1.\2.\3.\4.\5',sys.argv[1])
print phone
========

=Python displays "...." instead. Any idea what I'm doing wrong?

Looks like you need to use "raw" strings of the form

r'...'

Otherwise, you'd need to escape every backslash so that the
regexp parser actually gets it. Thus you can either do this:

p = re.compile(r'(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)')
phone = p.sub(r'\1.\2.\3.\4.\5',sys.argv[1])

or the much more hideous:

p = re.compile('(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\ d)')
phone = p.sub('\\1.\\2.\\3.\\4.\\5',sys.argv[1])

HTH,

-tkc
Nov 21 '07 #2
On Wed, 2007-11-21 at 03:24 +0100, Gilles Ganault wrote:
Hello

I've been reading tutorials on regexes in Python, but I still
don't get it:

========
#!/usr/bin/python

#myscript.py 0123456789

import sys,re

#Turn 0123456789 into 01.23.45.67.89
p = re.compile('(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)')
phone = p.sub('\1.\2.\3.\4.\5',sys.argv[1])
print phone
========

=Python displays "...." instead. Any idea what I'm doing wrong?

Thank you.
Use raw strings for re expressions.

r'this is a raw string'
'this is not'

p = re.compile(r'(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)')
phone = p.sub(r'\1.\2.\3.\4.\5',sys.argv[1])

Should clear up your problem.

Nov 21 '07 #3
On Tue, 20 Nov 2007 21:57:43 -0500, "J. Clifford Dyer"
<jc*@sdf.lonestar.orgwrote:
>Use raw strings for re expressions.
Thanks guys for the prompt reply :-) Solved it.
Nov 21 '07 #4

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

Similar topics

12
by: chris | last post by:
i can see the power of regular expressions but am having a bit of a battle getting my head around them. can anyone recommend some BASIC - tutorials for using regex something like th idots...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
2
by: Daniel Billingsley | last post by:
First, if MSFT is listening I'll say IMO the MSDN material is sorely lacking in this area... it's just a whole bunch of information thrown at you and you're left to yourself as to organizing it in...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Dave | last post by:
#define BOOST_REGEX_DYN_LINK #include <iostream> #include <boost\regex.hpp> using namespace boost; using namespace std; int main() {
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
4
by: Sarah | last post by:
Hi - I am extremely new to programming so I apologize in advance for my lack of knowlege, vocabulary and if my code is sloppy. I will take any suggestions to clean up my code and to follow any...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.