25

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?

q3d
  • 3,473
  • 8
  • 34
  • 39
  • 3
    Without locale indication alphanumeric characters match only `[a-zA-Z0-9]+`, not `[\w]+`. – greg Oct 28 '13 at 08:27

4 Answers4

39

If you want to test a string against a regular expression, use the re library

import re
valid = re.match('^[\w-]+$', str) is not None
math
  • 2,811
  • 3
  • 24
  • 29
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
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