Regular expression solution

Brian Brian.Mingus at colorado.edu
Tue Jul 28 23:42:31 EDT 2009


On Tue, Jul 28, 2009 at 9:11 PM, tiefeng wu <icebergwtf at gmail.com> wrote:

> 2009/7/29 Nobody <nobody at nowhere.com>:
> >> 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)
> >
>
> 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>'
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 >>> from time import time>>> import re>>>
s='<script>document.write("h" +"e"+ "ll"+ "ooooo"+ " m" +"y"+"
n"+"ame"+" is  "+"21c";)</script>'>>> *# my version*>>>
start=time();null=s.replace(' +','+').replace('+
','+').replace('+','').replace('"','').replace('(','("').replace(';)','";)');*print*
time()-start2.8133392334e-05>>> *# nobody's version*>>> r =
re.compile(r'"\s*\+\s*"')>>> start = time();r.sub('',s);*print*
time()-start3.40938568115e-05>>> *# tiefeng's version*>>>
start=time();re.sub(r'(?<=")(.+)(?=")',
''.join(re.findall(r'"([^"]+)"', s)), s);*print*
time()-start0.000455141067505>>>
3.40938568115e-05/2.8133392334e-051.2118644067781548>>>
0.000455141067505/2.8133392334e-0516.177966101690096>>>
0.000455141067505/3.40938568115e-0513.349650349662966
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090728/6e464d39/attachment.html>


More information about the Python-list mailing list