Bart Friederichs
Bart Friederichs

Reputation: 33533

Match charactes and whitespaces, but not numbers

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

Answers (2)

ctwheels
ctwheels

Reputation: 22837

Brief

It's unclear what exactly characters refers to, but, assuming you mean alpha characters (based on your input), this regex should work for you.


Code

See regex in use here

^(?:(?!\d)[\w ])+$

Note: This regex uses the mu flags for multiline and Unicode (multiline only necessary if input is separated by newline characters)


Results

Input

ÀÇÆ some words
ÀÇÆ some words 123

Output

This only shows matches

ÀÇÆ some words

Explanation

  • ^ 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 line

Upvotes: 1

Jan
Jan

Reputation: 43169

You can use a lookahead:

(?=^\D+$)[\w\s]+


In 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

Related Questions