The string I'm testing can be matched with [\w-]+
. Can I test if a string conforms to this in Python, instead of having a list of the disallowed characters and testing for that?
Asked
Active
Viewed 4.8k times
25

q3d
- 3,473
- 8
- 34
- 39
-
3Without locale indication alphanumeric characters match only `[a-zA-Z0-9]+`, not `[\w]+`. – greg Oct 28 '13 at 08:27
4 Answers
8
Python has regex as well:
import re
if re.match('^[\w-]+$', s):
...
Or you could create a list of allowed characters:
from string import ascii_letters
if all(c in ascii_letters+'-' for c in s):
...

Lev Levitsky
- 63,701
- 20
- 147
- 175
-
re.match('^[\w-]+$', s) return a corresponding MatchObject instance. Return None if the string does not match the pattern. Should be compared with None. – Temak Nov 16 '13 at 15:21
-
2@Enor Why? Any match object will be truthy, None is the only possible falsy value. – Lev Levitsky Nov 16 '13 at 17:34
8
Without importing any module just using pure python, remove any none alpha, numeric except dashes.
string = '#Remove-*crap?-from-this-STRING-123$%'
filter_char = lambda char: char.isalnum() or char == '-'
filter(filter_char, string)
# This returns--> 'Remove-crap-from-this-STRING-123'
Or in one line:
''.join([c for c in string if c.isalnum() or c in ['-']])

Diego Suarez
- 901
- 13
- 16
-
3This should be the accepted answer, you do not need regex or external packages to solve this. – roganartu Dec 13 '17 at 15:44
-
1
1
To test if the string contains only alphanumeric and dashes, I would use
import re
found_s = re.findall('^[\w-]+$', s)
valid = bool(found_s) and found_s[0] == s

richard
- 547
- 6
- 18