473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1241
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: H Branyan | last post by:
I need to format a textbox value a user enters. The user will enter 13 characters, and then I have to format them, preferably in the onBlur event, to look like this: XXXX-XX-XXX-XXXX I found...
10
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: ...
6
by: WindAndWaves | last post by:
Hi Gurus In my quest in putting my first javascript together, I am now trying to conquer something that seems trivial, but has taken me hours. I would like to format a field in a form once the...
6
by: coriolis_wong | last post by:
Hi, I need to transfer csv format file to DBase III format file. How do i do it in Python language? Any help is appreciated. Thanks.
15
by: angellian | last post by:
Sorry to raise a stupid question but I tried many methods which did work. how can I conserve the initial zero when I try to convert STR(06) into string in SQL statment? It always gives me 6...
24
by: Daniel Rudy | last post by:
Hello Group, Consider the following code: /* form 1 */ int main(void) { int i; char *p;
15
by: Liviu | last post by:
Say i have a function: char *rmtrail(char *str) { if (str) { int i; for (i = strlen(str) - 1; i >= 0 && isspace(str); --i) ;
5
by: hirsh.dan | last post by:
Hello to all, i have the following functions: string File::readLine(){ char ch; string str; ch = read(); while(ch != LF && ch != CR && ch != -1){ str.append(1,ch); ch = read(); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.