All your web development code goes here

Archive for the ‘preg_match’ Category

Regex in PHP

1. 1 Character 1 number and string length


  • ^: anchored to beginning of string
  • \S*: any set of characters
  • (?=\S{8,}): of at least length 8
  • (?=\S*[a-z]): containing at least one lowercase letter
  • (?=\S*[A-Z]): and at least one uppercase letter
  • (?=\S*[\d]): and at least one number
  • $: anchored to the end of the string

To include special characters, just add (?=\S*[\W]), which is non-word characters



   * contain at least (1) upper case letter
* contain at least (1) lower case letter
* contain at least (1) number or special character
* contain at least (8) characters in length


pregmatch for shell script

*     matches any string of zero or more characters
?     matches any single character
[ ]   matches any single character within the set
[! ]  matches any single character not in the set

These can be combined together to form more advanced patterns:
[Yy]* Matches any string starting with an upper or lower case y.

Use quotes or escape/back-slash the special characters if you wish to pattern match them specifically.

PHP Number and strings validations

1. to check a valid phone number for Pakistan e.g 03001234567

    $numberFormat = preg_match($Msisdn_pattern , $phoneNumber);

2. Format a number for proper usage



%d bloggers like this: