Python Regex for Words & single space
I am using re.sub in order to forcibly convert a "bad" string into a
"valid" string via regex. I am struggling with creating the right regex
that will parse a string and "remove the bad parts". Specifically, I would
like to force a string to be all alphabetical, and allow for a single
space between words. Any values that disagree with this rule I would like
to substitute with ''. This includes multiple spaces. Any help would be
appreciated!
import re
list_of_strings = ["3He2l2lo Wo45rld!", "Hello World- -number two-",
"Hello World number .. three"
for str in list_of_strings:
print re.sub(r'[^A-Za-z]+([^\s][A-Za-z])*', '' , str)
I would like the output to be:
Hello World
Hello World number two
Hello World number three
No comments:
Post a Comment