469,328 Members | 1,260 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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

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.

Mar 25 '06 #1
2 1125
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
Mar 25 '06 #2
"dongdong" <do***********@hotmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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.


Well, here's the pyparsing rendition (two, actually). I hope it's easier to
follow then trying to pick apart the above regexp. Both

Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul
instr = """' asd ga<a target="_blank"
href="http://www.sine.com" class="wordstyle"> asdgasdghae rha</a>'"""

from pyparsing import makeHTMLTags,Suppress,MatchFirst

# makeHTMLTags returns a tuple of the start and end pattern for the given
tag
aStart,aEnd = makeHTMLTags("a")

# define a filter that will suppress the opening and closing tags
# this is easily extended to filter additional patterns, too
filter = Suppress(aStart) | Suppress(aEnd)

# invoke transformString using the filter
print filter.transformString(instr)

# or for the dense-code minded...
filter = MatchFirst( map( Suppress, makeHTMLTags("a") ) )
print filter.transformString(instr)
Prints:
' asd ga asdgasdghae rha'
' asd ga asdgasdghae rha'
Mar 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by H Branyan | last post: by
10 posts views Thread by Colin Steadman | last post: by
6 posts views Thread by WindAndWaves | last post: by
6 posts views Thread by coriolis_wong | last post: by
24 posts views Thread by Daniel Rudy | last post: by
15 posts views Thread by Liviu | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.