python - Regex to include alphanumeric and _ -
i'm trying create regular look match alphanumeric characters , underscore _
. regex: "\w_*[^-$\s\]"
, impression regex means alphanumeric character \w
, underscore _
, , no -
,$
, or whitespace. correct?
regular expressions read patterns match characters in string, left right, pattern matches alphanumeric, underscore (0 or more), @ to the lowest degree 1 character not hyphen, dollar, or whitespace.
since you're trying alternate on character types, utilize character class show characters you're allowing:
[\w_]
this checks part of string matches it, let's anchor origin , and of string:
^[\w_]$
and see character class lacks quantifier, matching on 1 character. can prepare using +
(if want 1 or more characters, no empty strings) or *
(if want allow empty strings). i'll utilize +
here.
^[\w_]+$
as turns out, \w
character class includes underscore, can remove redundant underscore pattern:
^[\w]+$
and have 1 character in character class, no longer need character class brackets @ all:
^\w+$
and that's need, unless i'm missing requirements.
python regex
No comments:
Post a Comment