[regex] Basic rewriting
J. Clifford Dyer
jcd at sdf.lonestar.org
Tue Nov 20 21:57:43 EST 2007
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.
More information about the Python-list
mailing list