Regular expression solution

N

nickname

Hello all,
I'm a newbie with python. I am looking to write a regex
program which can take in a piece of text and remove some parts of it
and supply the remaining parts back.

The input can be something like:
<script>document.write("h" +"e"+ "ll"+ "ooooo"+ " m" +"y"+" n"+"ame"+
" is "+"21c";)</script>

The output should be something like
<script>document.write("hellooooo my name is 21c";)</script>

I have tried and failed. I am reading http://www.amk.ca/python/howto/regex/
and hope to get somewhere with this soon. If this is something simple,
can someone please help me out.

Thanks in advance for any help.
 
N

Nobody

I'm a newbie with python. I am looking to write a regex
program which can take in a piece of text and remove some parts of it
and supply the remaining parts back.

The input can be something like:
<script>document.write("h" +"e"+ "ll"+ "ooooo"+ " m" +"y"+" n"+"ame"+
" is "+"21c";)</script>

The output should be something like
<script>document.write("hellooooo my name is 21c";)</script>

import re
r = re.compile(r'"\s*\+\s*"')
s = r'''<script>document.write("h" +"e"+ "ll"+ "ooooo"+ " m" +"y"+" n"+"ame"+
" is "+"21c";)</script>'''
r.sub('', s)
 
T

tiefeng wu

2009/7/29 Nobody said:
import re
r = re.compile(r'"\s*\+\s*"')
s = r'''<script>document.write("h" +"e"+ "ll"+ "ooooo"+ " m" +"y"+" n"+"ame"+
" is  "+"21c";)</script>'''
r.sub('', s)

Nobody's solution is good.
here is more complicated solution, just for regex practice :)
s = '<script>document.write("h" +"e"+ "ll"+ "ooooo"+ " m" +"y"+" n"+"ame"+" is "+"21c";)</script>'
new_s = re.sub(r'(?<=")(.+)(?=")', ''.join(re.findall(r'"([^"]+)"', s)), s)
new_s
'<script>document.write("hellooooo my name is 21c";)</script>'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top