Reputation: 33533
I am trying to create a regex that will match characters, whitespaces, but not numbers.
So hello 123
will not match, but hell o
will.
I tried this:
[^\d\w]
but, I cannot find a way to add whitespaces here. I have to use \w
, because my strings can contain Unicode characters.
Upvotes: 1
Views: 60
Reputation: 22837
It's unclear what exactly characters refers to, but, assuming you mean alpha characters (based on your input), this regex should work for you.
^(?:(?!\d)[\w ])+$
Note: This regex uses the mu
flags for multiline and Unicode (multiline only necessary if input is separated by newline characters)
ÀÇÆ some words
ÀÇÆ some words 123
This only shows matches
ÀÇÆ some words
^
Assert position at the start of the line(?:(?!\d)[\w ])+
Match the following one or more times (tempered greedy token)
(?!\d)
Negative lookahead ensuring what follows doesn't match a digit. You can change this to (?![\d_])
if you want to ensure _
is also not used.[\w ]
Match any word character or space (matches Unicode word characters with u
flag)`$
Assert position at the end of the lineUpvotes: 1
Reputation: 43169
You can use a lookahead:
(?=^\D+$)[\w\s]+
Python
:
import re
strings = ['hello 123', 'hell o']
rx = re.compile(r'(?=^\D+$)[\w\s]+')
new_strings = [string for string in strings if rx.match(string)]
print(new_strings)
# ['hell o']
Upvotes: 0