Reputation: 751
I'm trying to create code that will take text input, eliminate all whitespace characters (left and right, not those between characters) and then validate if the string has following format: 'XXX XXX' (where X is letter or digit). If it doesn't (say that it has XXX-XXX or XXXXXX format) it would convert it to such format.
I know that I can use strip
to remove whitespace from a string. I also know how to check the length of a string and how to convert it, but I don't know how to validate a string after using .strip()
.
Upvotes: 1
Views: 132
Reputation:
Use regular expressions.
>>> import re
>>> inp.strip()
aB8-9uG
>>> match = re.match(r"([0-9a-zA-Z]{3})(?:\-| |)([0-9a-zA-Z]{3})", _)
>>> match
<_sre.SRE_Match object; span=(0, 7), match='aB8-9uG'>
>>> match.group(1) + " " + match.group(2)
aB8 9uG
Upvotes: 1