Regular expression solution

tiefeng wu icebergwtf at gmail.com
Wed Jul 29 05:11:28 CEST 2009


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>'



More information about the Python-list mailing list