Why is this regex allowing a caret? -


http://regexr.com/3ars8

^(?=.*[0-9])(?=.*[a-z])[0-9a-z-]{17}$ 

should match "17 alphanumeric chars, hyphens allowed too, must include @ least 1 letter , @ least 1 number"

it'll correctly match:

abcdf31u100027743 

and correctly decline match:

ab$df31u100027743 

(and other non-alphanumeric char)

but apparently allow:

ab^df31u100027743 

because character class [a-z] matches symbol.

[a-z] matches [, \, ], ^, _, ` , english letters.

actually, common mistake. should use [a-za-z] instead allow english letters.

here visualization expresso, showing range [a-z] covers:

screenshot expresso showing ascii table, can see [a-z] range covers

so, this regex (with i option) won't capture string.

^(?=.*[0-9])(?=.*[a-z])[0-9a-z-]{17}$ 

in opinion, safer use ignorecase option avoid such issue , shorten regex.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -