fbpx

Request a Call back


captcha

Learn to Use Regular Expressions to Search Patterns in PHP

Regular expression is a sequence of characters that are used for searching specific patterns matching the sequence in a given text. POSIX and Perl Compatible Regular Expressions are two widely used forms of regular expressions that are used in PHP.

Let us now see in detail what are regular expressions and how are they used in PHP.

Regular expression is a sequence of characters which when written in a parser-understandable format helps in searching for particular type of patterns matching the sequence in a given text. One can search, replace and split the strings using these regex or regexp functionalities in hypertext preprocessor.

Even complex problems can be solved easily with use of these expressions. For example when you are required to search for an email-id from a long list of ids provided, it is quite a simple task to do with search patterns. It will not take longer if you are an expert at framing these regex statements for search. While on the other hand doing it manually would be a time consuming task.

There are two types in regular expressions as used by the users or programmers of hypertext preprocessors.

POSIX regular expressions

Perl Compatible Regular Expressions

  • POSIX Regular Expressions:

POSIX regex is a form of expression just similar to an arithmetic expression. It combines various elements to form complex expressions and is used to determine various set of characters from the given text.

Following are a few POSIX regex techniques used by the programmers for searching patterns of text.

1. Use of Brackets [] – Brackets in regex are used for finding a range of characters which could be the range of numbers or decimal digits [0-9], lowercase alphabets [a-z] and uppercase alphabets [A-Z].

2. Quantifiers – These are used for finding frequency or position of a single character or sequence of characters. Special characters like +, *, ?, {int. range}, and $ flags are used after the character sequence in order to guide the type of search.

For example: Consider p+. This will match a string containing atleast one p. Similarly, p* matches string containing zero or more p’s. p{N} matches a string containing a sequence of N p’s, p$ will match string with p at the end of it while ^p matches a string with p at the beginning of it.

3. Predefined Character Ranges – This matches a string with specific or any character as in the character range mentioned. For example: [[:digit:]] will match any string that contains digits through 0 to 9, while [[:alpha:]] matches the string containing alphabetic characters aA through zZ.

POSIX String Search Functions

Following are seven functions used for string search in POSIX style regexp.

Function

Description

 ereg() This will evaluate a search for string and will return true if the pattern matching the string is  found, and false if not.
 ereg_replace() This will search for pattern matching the string mentioned and replaces that pattern with string if found.
 eregi_replace() This works in similar fashion as that of ereg_replace(). But here the search is not case sensitive.
 split() It slices the string into number of elements boundaries of which are based on the occurrence of pattern in string.
 spliti() It is similar to split(), but is not case sensitive.
 sql_regcase() This will transform every character in the input parameter string into a bracketed expression containing two characters.

  • Perl Compatible Regular Expressions

Perl Compatible Regular Expressions or PCRE are just the counterparts of POSIX expressions which can be used interchangeably.

   Following are a few concepts of PCRE used by the programmers for searching patterns of text.

1. Using the Metacharacters – A Metacharacter includes a backslash followed by an alphabet which together produces a unique meaning for searching pattern of text.

For example: Consider /s represents a whitespace character while /S is for non-white space characters. On the other hand /d represents all the digits, /w represents a word character.

You can match more than one character by mentioning set or use alternative matches. For example, {abcd} will match any character in the given set while (abc|def|ghi) matches string containing any of the alternatives specified.

2. Operators – Different operators as basic building blocks of regular expressions in PHP.

For example: Symbol ‘?’ will match it’s preceding pattern zero or one time while ‘+’ will do the same but for one or more times. ‘-‘ matches a range of elements and [] matches any single character from the characters as mentioned between the square brackets.

Important PCRE Functions

Following are the six functions used for string search using PCRE regex.

FunctionDescription
 preg_match() It searches string for pattern and thereby returns true if the pattern exists, and false if not.
 preg_replace() Searches for string as mentioned in the search pattern and replaces pattern with replacement if found. The regular expressions can be used in the pattern and replacement input parameters.
 preg_split() This is same as split(), difference is that here the regular expressions are accepted as input parameters for pattern.
 preg_grep() The preg_grep() function searches all elements of input_array and returns all those that match the regexp pattern.
 preg_ quote() Quote regular expression characters

The widely used of these are the preg_match() and preg_replace().

TOPS Technologies is a leading PHP training institute offering the best courses contents to train individuals with live project work. The institute has got state-of-art infrastructure to support the learning process and also provides complete study material for reference of the students so that they can get better understanding of concepts.

Leave a Reply

Your email address will not be published.

Leave a Reply

Your email address will not be published. Required fields are marked *

Need Answers

Now???

Our Counsellors can Answer Any Queries.

 Just Request a Call

Thank you

Calling you Soon