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

Paul McGuire ptmcg at austin.rr._bogus_.com
Sat Mar 25 11:42:43 EST 2006


"dongdong" <dongdonglove8 at hotmail.com> wrote in message
news:1143276718.439741.109220 at u72g2000cwu.googlegroups.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'





More information about the Python-list mailing list