Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php regex needed to check that a string has at least one uppercase char, one lower case char and either one number or symbol

Tags:

regex

php

Hi I need to use php's pregmatch to check a string is valid. In order to be valid the string needs to have at least one uppercase character, at least one lowercase character, and then at least one symbol or number

thanks

like image 339
geoffs3310 Avatar asked Jan 18 '23 01:01

geoffs3310


1 Answers

You can achieve this by using lookaheads

^(?=.*[a-z])(?=.*[A-Z])(?=.*[\d,.;:]).+$

See it here on Regexr

A lookahead is a zero width assertion, that means it does not match characters, it checks from its position if the assertion stated is true. All assertions are evaluated separately, so the characters can be in any order.

^ Matches the start of the string

(?=.*[a-z]) checks if somewhere in the string is a lowercase character

(?=.*[A-Z]) checks if somewhere in the string is a uppercase character

(?=.*[\d,.;:]) checks if somewhere in the string is a digit or one of the other characters, add those you want.

.+$ Matches the string till the end of the string

As soon as one of the Assertions fail, the complete regex fail.

like image 84
stema Avatar answered Apr 05 '23 23:04

stema