PHP-Regular Expression

    A regular expression can be considered as the sequence of characters that allows you to use the PHP functionality of pattern matching. Regular expression helps you to search for a particular string, replace one string from another string. PHP supports two types of regular expressions on which you can perform different functions.

    • POSIX regular expression
    • PERL style regular expression

    POSIX regular expression

    This POSIX regular expression works almost the same way as the typical arithmetic expression works where one or more than one elements combined together to create complex expressions. There are many concepts that you need to learn before starting with POSIX regular expression functions. Basic concepts of POSIX regular expression-

    1. Brackets

    when you are dealing with regular expression then brackets plays an important role and can be used to set a range for the characters.

    • [0-9]- matches decimal digit from 0 through 9.
    • [a-z]- matches lowe-case characters from a through z.
    • [A-Z]- matches upper-case characters from A through Z.
    • [a-Z]- matches lower-case a through upper-case Z.

    These are the notations only but you can use digits or any other alphabets or characters to define the string range.

    2. Quantifiers

    You can use the quantifiers in case if you want to look for some occurrence of a character sequence or a single character. You can make use of special characters like +, *,? and $ to mark the character occurrence. Some of the working expressions are as below.

    • P+ will match a string with at least one P character.
    • p* will match a string for having zero or more p.
    • P? Will match for string having zero or one P.
    • p{N} will match the string having N occurrence of p.
    • P{2,3} will match the string having 2 or 3 sequences of P.
    • P{2,} will match the string having at least 2 sequences of P.
    • p$ will match the string ending with one p.
    • ^p will match the string starting with p.

    3. Predefined character ranges

    PHP supports character classes that help you to set a character range for the ease of programming. These classes allow you to use an entire range of characters.

    • [[: alpha:]]- will match the string for alphabet characters aA through zZ.
    • [[: digit:]]- matches the string with digits 0 through 9.
    • [[:alnum:]]- it matches the string with a combination of alpha and digit.
    • [[: space:]]- will match the string with space.

    Now moving towards the POSIX functions that allows you to search strings. PHP supports seven different functions to solve the purpose.

    POSIX functions

    Function Description
    ereg() This function searches for a specified pattern within the string which returns true if matched else return false.
    ereg_replace() This function replaces the pattern for the matched pattern within the string.
    eregi() This function searches for a specified pattern within the string and is case-sensitive which returns true if matched else return false.
    eregi_replace() This function replaces the pattern for the matched pattern within the string. The search for the pattern is case-sensitive.
    split() This function will split the string which is based on the occurrence of pattern in a string.
    spliti() This function will split the string which is based on the occurrence of pattern in a string, considering the search is case-sensitive.
    sql_regcase() It is a utility function that converts each character of the input or provided string into a bracketed expression.

    PERL style regular expression

    PERL style regular expression works almost the same as the POSIX regular expression. Even you can use POSIX concepts for searching a string with PERL style. There are some basic concepts of PERL style you should know before continuing with the PERL functions. Basic concepts

    1. Meta characters

    These meta characters provide some special meaning that is denoted by an alphabet along with a backslash.

    • . - denotes a single character
    • \s - denotes a whitespace character (space, tab, newline)
    • \S -denotes non-whitespace character
    • \d -denotes a digit (0-9)
    • \D -denotes a non-digit
    • \w -denotes a word character (a-z, A-Z, 0-9, _)
    • \W -denotes a non-word character
    • [aeiou] -denotes matches a single character in the given set
    • [^aeiou] -denotes matches a single character outside the given set
    • (foo|bar|baz) - denotes matches any of the alternatives specified

    2. Modifiers

    You can use modifiers as they allow you to work with a regular expression more easily.

    • i -allows the match case insensitive
    • m -it checks if the string has newline or carriage return characters, where ^ and $ operators will check against a newline boundary, instead of a string boundary
    • o -it will evaluate the expression only once
    • s -it allows you to match a newline character
    • x -it allows you to use white space in the expression for clarity
    • g -Globally which is used to finds all matches
    • cg -it will allow a search to continue even after a global match fails

    PERL functions

    PHP supports below functions-

    Function Description
    preg_match() This function will check for a pattern and return true is found.
    preg_match_all() This function will match all occurrences of a particular pattern.
    preg_replace() This function will search for a pattern and replace it with the input replacement parameter.
    preg_split() This function will split the string according to pattern and expression can also be used as a parameter.
    preg_grep() This functions will return all elements matching the regular expression
    preg_quote() This function will quote the regular expression.

    People are also reading: