how to format a return value by using re.sub(regx,rep1,str)?

Kent Johnson kent at kentsjohnson.com
Sat Mar 25 13:03:19 CET 2006


dongdong wrote:
> for example:
>  re.sub('<a( [^>]+)+\s?>[^<^>]*</a>','',' asd ga<a target="_blank"
> href="http://www.sine.com" class="wordstyle"> asdgasdghae rha</a>')
> 
> I wish to get the return value "asd ga asdgasdghae rha",how do do?
> I have a impression on "%" and "{number}",but forgot how to use them.
> 
Use a group to capture the text between <a> and </a>:

In [10]: re.sub('<a( [^>]+)+\s?>([^<^>]*)</a>',r'\2',' asd ga<a 
target="_blank" href="http://www.sine.com" class="wordstyle"> 
asdgasdghae rha</a>')
Out[10]: ' asd ga asdgasdghae rha'

Kent



More information about the Python-list mailing list