Just another WordPress.com site

Archive for the ‘preg_match’ Category

Regex in PHP

1. 1 Character 1 number and string length

^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$

  • ^: 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

OR

(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$

   * 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

References
1. http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm
2. http://www.regular-expressions.info/index.html

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

    $number_pattern=”/^3[0-9]{9}$/”;
    $phoneNumber=03465315155
    $numberFormat = preg_match($Msisdn_pattern , $phoneNumber);

2. Format a number for proper usage

    $phoneNumber=03465315155;
    $phoneNumber=’0092′.substr($_REQUEST[“M”],-10);

   

%d bloggers like this: