Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient regex for checking if a string contains at least 3 alphanumeric characters

I have this regex:

(?:.*[a-zA-Z0-9].*){3}

I use it to see if a string has at least 3 alphanumeric characters in it. It seems to work.

Examples of strings it should match:

'a3c'
'_0_c_8_'
' 9 9d '

However, I need it to work faster. Is there a better way to use regex to match the same patterns?


Edit: I ended up using this regex for my purposes:

(?:[^a-zA-Z0-9]*[a-zA-Z0-9]){3}

(no modifiers needed)

like image 989
michen00 Avatar asked Jun 09 '15 05:06

michen00


1 Answers

The most efficient regex approach is to use the principle of contrast, i.e. using opposite character classes side by side. Here is a regex that can be used to check if a string has 3 Latin script letters or digits:

^(?:[^a-zA-Z0-9]*[a-zA-Z0-9]){3}

See demo.

In case you need a full string match, you will need to append .* (or .*$ if you want to guarantee you will match all up to the end of string/line), but in my tests on regexhero, .* yields better performance):

^(?:[^a-zA-Z0-9]*[a-zA-Z0-9]){3}.*

Also, a lot depends on the engine. PCRE has auto-optimizations in place that consists in auto-possessification (i.e. it turns the * to *+ in (?:[^a-zA-Z0-9]*+).

See more details on password validation optimizations here.

like image 124
Wiktor Stribiżew Avatar answered Sep 27 '22 17:09

Wiktor Stribiżew