Reputation: 189
In my actual project I want to get some input from a user. It cannot contain whitespace at the beginning, at the end or twice or more in a row and it also cannot contain anything that is not a letter or a number.
However I have 2 problems:
\W
disallows whitespaces. I want to allow one but not in the beginning, the end or more than once.\W
disallows it. I can't replace it with [^a-zA-Z0-9\s\s.]
because it disallows characters like 'äöüß' which I want to allow. How can I change my code to disallow everything that is in the pattern below except for single whitespaces and dots?input_text = input("input: ")
if re.search(r"(^\s|\s{2,}|\s$|\W)", input_text)
print("invalid input")
Upvotes: 1
Views: 208
Reputation: 626927
You may use either of the two:
^[^\W_]+(?:[\s.][^\W_]+)*\Z
^(?:[^\W_]|\.)+(?:\s(?:[^\W_]|\.)+)*\Z
The first one will match single dots and whitespace only in between letters. The second one allows dots to be anywhere in the string and in any consecutive quantities. See regex #1 demo and regex #2 demo.
Details
^
- start of string[^\W_]+
- one or more Unicode letters or digits(?:[^\W_]|\.)+
- a non-capturing group matching one or more Unicode letters/digits or a dot(?:[\s.][^\W_]+)*
- zero or more repetitions of
[\s.]
- a single whitespace or dot[^\W_]+
- one or more Unicode letters or digits\Z
- the very end of string.In Python, use something like
if re.search(r'^[^\W_]+(?:[\s.][^\W_]+)*\Z', input_text):
print('Valid')
Upvotes: 1