Syntax – Regular Expression

Know About >> Reg Exp

  1. $ : dollar sign ($) is used to match strings that end with the given pattern.
  2. ^ : a caret (^) character at the beginning of a regular expression indicates that it must match the beginning of the string.
  3. The characters that match themselves are called literals.
  4. The characters that have special meanings are called metacharacters.
  5. (.) : The dot (.) metacharacter matches any single character except newline (\). So, the pattern h.t matches hat, hothit, hut, h7t, etc.
  6. The vertical pipe (|) metacharacter is used for alternatives in a regular expression. It behaves much like a logical OR operator and you should use it if you want to construct a pattern that matches more than one set of characters.  For instance, the pattern Utah|Idaho|Nevada matches strings that contain “Utah” or “Idaho” or “Nevada”.
  7. Parentheses give us a way to group sequences. For example, (Nant|b)ucket matches “Nantucket” or “bucket”. Using parentheses to group together characters for alternation is called grouping.
  8. / : If you want to match a literal metacharacter in a pattern, you have to escape it with a backslash.
  9. A character class matches any one of the characters in the class. For example a character class [abc] matches a, b or c. To define a range of characters, just put the first and last characters in, separated by hyphen. For example, to match all alphanumeric characters: [a-zA-Z0-9]. You can also create a negated character class, which matches any character that is not in the class. To create a negated character class, begin the character class with ^: [^0-9].
  10. The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched. + means “Match one or more of the preceding expression” * means “Match zero or more of the preceding expression” ? means “Match zero or one of the preceding expression”.
  11. Curly braces {} can be used differently. With a single integer,  {n} means “match exactly n occurrences of the preceding expression” with one integer and a comma, {n,} means “match n or more occurrences of the preceding expression” with two comma-separated integers {n,m} means “match the previous character if it occurs at least n times, but no more than m times”

$password = “Fyfjk34sdfjfsjq7”;

if (preg_match(“/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/”, $password)) {
echo “Your passwords is strong.”;
} else {
echo “Your password is weak.”;

$email =;
$regexp = “/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/”;

if (preg_match($regexp, $email)) {
echo “Email address is valid.”;
} else {
echo “Email address is <u>not</u> valid.”;

Regular Expression to check >>

Floating number

if (!preg_match(‘/^\d+(?:\.\d{1,2})?$/’, $couponvalue))


if (!preg_match(‘/^\d+$/’, $minCartValue))

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: