python re.sub
Ivan Van Laningham
ivanlan at callware.com
Thu May 6 15:46:59 EDT 1999
Hi Jerry--
"Jerry L. Bash" wrote:
>
> Ivan,
>
> I am very new to python and have a question regarding use of regular
> expressions substitutions. I am using the new re module and am unable to
> reproduce one of the examples given in the HOWTO on www.python.org.
>
I've taken the liberty of cc'ing this to the Python mailing list; you
can find out more about how to sign up at www.python.org.
> Maybe I have a faulty installation, or some other misunderstanding?
>
> At any rate, the following is mostly taken from the HOWTO. It appears
> that the default 'count' is not working as I expected, resulting in
> not making substitutions for all cases.
>
> Python 1.5.2 (#2, Apr 26 1999, 09:21:48) [GCC 2.8.1] on irix646
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> import re
> >>> p = re.compile( '(blue|white|red)')
> >>> p.sub( 'colour', 'blue socks and red shoes')
> 'blue socks and red shoes'
> >>> p.sub( 'colour', 'blue socks and red shoes', 1)
> 'colour socks and red shoes'
> >>> p.sub( 'colour', 'blue socks and red shoes', 2)
> 'colour socks and colour shoes'
> >>> p.sub( 'colour', 'blue socks and red shoes', 0)
> 'blue socks and red shoes'
> >>>
>
> Above, I expected the first p.sub to replace both 'blue' and 'red' with
> 'color', as in the HOWTO.
> The second p.sub works as expected, as does the third.
> However, I expected the fourth to again make substitutions for both 'blue'
> and 'red' again, having given 'count' the default value of 0.
>
> Being new, I imagine I have surely overlooked something, but if you
> can help I would greatly appreciate it.
>
Jerry, I ran your example on my NT system and on my Linux system. In
both cases, the re sub worked exactly as expected. However, my NT
Python says ``Python 1.5.2 (#0, Apr 13 1999, 10:51:12)...'' Perhaps
there is a problem with the particular build you've got. Probably,
someone else out there can explain the difference in results.
Anyone?
<great-expectations>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
----------------------------------------------
More information about the Python-list
mailing list