Additions:
====Split====
===Split on One or More Spaces===
>>> import re
>>> s = 'SAN DIEGO CA 92037 PAGE 2'
>>> re.split('\s+', s)
['SAN', 'DIEGO', 'CA', '92037', 'PAGE', '2']
===Split on One or More Spaces===
>>> import re
>>> s = 'SAN DIEGO CA 92037 PAGE 2'
>>> re.split('\s+', s)
['SAN', 'DIEGO', 'CA', '92037', 'PAGE', '2']
Additions:
===Basic alphanum-only example===
> s = 'John!Paul/9{}George'
> re.sub('[^0-9a-zA-Z]', '_', s)
'John_Paul_9__George'
===Replace extra spaces in XML===
> s = 'John!Paul/9{}George'
> re.sub('[^0-9a-zA-Z]', '_', s)
'John_Paul_9__George'
===Replace extra spaces in XML===
Deletions:
Additions:
return to DevPython
Additions:
""<tt>re.sub(pattern, repl, string[, count, flags])</tt>"" ([[http://docs.python.org/library/re.html#re.sub docs]])
Deletions:
Additions:
====Replace (Sub)====
""<tt>re.sub(pattern, repl, string[, count, flags])</tt>""
Replace extra spaces in XML
re_p = """\s{2,}"""
xml1 = """
<outer>
<inner attr="foo">
some text
</inner>
<outer>
"""
xml2 = re.sub(re_p, ' ', xml1).replace('> <', '><')
print xml2
""<tt>re.sub(pattern, repl, string[, count, flags])</tt>""
Replace extra spaces in XML
re_p = """\s{2,}"""
xml1 = """
<outer>
<inner attr="foo">
some text
</inner>
<outer>
"""
xml2 = re.sub(re_p, ' ', xml1).replace('> <', '><')
print xml2