python replace/sub/wildcard/regex issue

tom bedouglas at earthlink.net
Tue Jan 19 05:38:18 CET 2010


hi chris...

should the reply you sent me be implemented all on one line? like:

test='Soo Choi</span>LONGEDITBOX">Apryl Berney'
new_text = "\n".join(line[:line.index('<')] + line[line.rindex('>')+1:] for
line in test.split('\n'))

and what would line be?

thanks



-----Original Message-----
From: python-list-bounces+bedouglas=earthlink.net at python.org
[mailto:python-list-bounces+bedouglas=earthlink.net at python.org]On Behalf
Of Chris Rebert
Sent: Monday, January 18, 2010 8:32 PM
To: tom
Cc: python-list at python.org
Subject: Re: python replace/sub/wildcard/regex issue


On Mon, Jan 18, 2010 at 8:04 PM, tom <badouglas at gmail.com> wrote:
> hi...
>
> trying to figure out how to solve what should be an easy python/regex/
> wildcard/replace issue.
>
> i've tried a number of different approaches.. so i must be missing
> something...
>
> my initial sample text are:
>
> Soo Choi</span>LONGEDITBOX">Apryl Berney
> Soo Choi</span>LONGEDITBOX">Joel Franks
> Joel Franks</span>GEDITBOX">Alexander Yamato
>
> and i'm trying to get
>
> Soo Choi foo Apryl Berney
> Soo Choi foo Joel Franks
> Joel Franks foo Alexander Yamato
>
> the issue i'm facing.. is how to start at "</" and end at '">' and
> substitute inclusive of the stuff inside the regex...
>
> i've tried derivations of
>
> name=re.sub("</s[^>]*\">"," foo ",name)
>
> but i'm missing something...
>
> thoughts... thanks

"Some people, when confronted with a problem, think 'I know, I'll use
regular expressions.' Now they have two problems."

Assuming your sample text is representative of all your test:

new_text = "\n".join(line[:line.index('<')] +
line[line.rindex('>')+1:] for line in your_text.split('\n'))

Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list