Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for a random set of chars and digits

I am looking for a regex that matches only when it sees a string that is randomly filled by digits and chars.

For example, adfak332arg3 is allowed but 332352 and fagaaah are not allowed. .*[^\\s] looks fine for strings with only chars but how to fix it to accepts the desired strings and refuses the other two types?

like image 860
lonesome Avatar asked Jan 31 '26 21:01

lonesome


1 Answers

Use a positive lookahead (?=) to ensure that the string contains required characters.

^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]+$

Test this regex pattern here.

like image 91
Ωmega Avatar answered Feb 03 '26 11:02

Ωmega



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!