Saturday, 15 January 2011

python - Regex not beginning with number -



python - Regex not beginning with number -

how create regex matches alphanumerics without number @ beginning?

right have "^[0-9][a-za-z0-9_]"

for example, 1ab not match, ab1 match, 1_bc not match, bc_1 match.

there 3 things wrong you've written.

first, negate character class, set ^ inside brackets, not before them. ^[0-9] means "any digit, @ start of string"; [^0-9] means "anything except digit".

second, [^0-9] match anything isn't digit, not letters , underscores. want first character "is not digit, digit, letter, or underscore", right? while isn't impossible that, it's lot easier merge "is letter or underscore".

also, forgot repeat lastly character set. as-is, you're matching 2 characters, b1 work, b12 not.

so:

[a-za-z_][a-za-z0-9_]*

debuggex demo

in others words: 1 letter or underscore, followed 0 or more letters, digits, or underscores.

i'm not exclusively sure want, @ to the lowest degree if regex whole parser. example, in foo-bar, want bar matched? if so, in 123spam, want spam matched? it's trying write.

python regex

No comments:

Post a Comment