Regular Expression in PHP

Introduction

The regular expression concept arose in the 1950s and was invented by American mathematician Stephen Kleene. First of all regular expressions are used in Unix for pattern matching. PHP supports two types of regular expressions, such as POSIX-extended and Perl-Compatible Regular Expressions (PCRE). The PCRE functions are more powerful than the POSIX one, so I will explain both of them. Regular expressions are very useful for validation or pattern matching. The primary use of regular expressions is for pattern matching with strings, or string matching. Using regular expressions you can search a string or replace a string.

The PHP POSIX-regex functions

  1. ereg()
  2. ereg_replace()
  3. eregi()
  4. eregi_replace()
  5. split()
  6. spliti()
  7. sql_regcase()

The PHP Perl-Compatible Regular Expressions (PCRE) functions

  1. preg_ match()
  2. preg_ replace()
  3. preg_ split()
  4. preg_ grep()
  5. preg_ filter()
  6. preg_ match_ all()
  7. preg_ replace_ callback()
  8. preg_ quote()
  9. preg_ last_ error()

Delimiters

When we use the PCRE function we need to enclose the pattern in delimiters. A delimiters are non-alphanumeric, non-backslash, non-white space characters, such as forward slash (/), hash sign (#), and tilde (~).

Meta characters

Meta Character

will Match

( Start sub pattern
) End sub pattern
[ Start character class definition
] End character class definition
{ Start min/max quantifier
} End min/max quantifier
\ Escape character with several uses
^ Assert start of subject
? Specified by quantifier zero or one
| Start of the alternative branch
+ Match One or more time quantifier
- Indicates character range
* Match Zero or more time quantifier
$ Assert end of subject or before a terminating new line
{n}? Match exactly n times
{n, }? Match at least n times
{n, m}? Match from n to m times
\d match digit (0-9)
\D match non-digit
\w match word character (a-z, A-Z, 0-9, _)
\W match non-word character
. match single character
\s match white space character (space, tab, new line)
\S match non-white space character

Modifiers

You can use several modifies for pattern matching.

Modifiers

Description

i In this modifier letters in the pattern match both upper and lower case letters
m Specifies that if the string has new line or carriage return characters
o Evaluates the expression only once
s In this modifier is set dot meta character in the pattern to match a new line character
x In this modifier is set you white space in the expression for clarity
g Globally finds all matches
cg Allows a search to continue even after a global match fails

Useful Patterns

The following are some sample patterns.

Pattern

Will Match

hello The string "hello"
^hello "hello" match at the start of a string
hello$ "hello" match at the end of a string
^hello$ "hello" when it is alone on a string
[xyx] x, y, or z
[a-z] match any lowercase letter
[^A-Z] match any character that is not a uppercase letter
(doc | csv) matches either "doc" or "csv"
[a-z]+ match one or more lowercase letters
[^A-Za-z0-9] match any symbol
([A-Z]{2}|[0-9]{2}) matches two letters or two numbers

Example

First of all I will explain the preg_match() function.

<?php

$subject = "raman";

$pattern = '/^an/';

preg_match($pattern, substr($subject,3), $treffer, PREG_OFFSET_CAPTURE);

echo "<pre>";print_r($treffer);

?>

 

Output

regular1.jpg

Example

This example shows the use of the preg_replace() function.

 

<?php

$subject = '30. september 2013';

$pattern = '/(\d+)\. (\w+) (\d+)/i';

$replace = '${2}1,$3';

echo preg_replace($pattern, $replace, $subject);

?>

 

Output

regular2.jpg

Example

This example shows the use of the preg_split() function.

 

<?php

$string = 'PHP Programming Language';

$split = preg_split('/ /', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);

print_r($split);

?>

 

Output

regular3jpg.jpg

Example

This example shows the use of the preg_filter() function.

 

<?php

$subject = array('2', 'a', '3', 'b', '4', 'A', 'B', '5');

$pattern = array('/\d/', '/[a-z]/', '/[2a]/');

$replace = array('A:$0', 'B:$0', 'C:$0');

//print filters data

echo "preg_filter example\n";

echo "<pre>";print_r(preg_filter($pattern, $replace,$subject));

//print riplaced data

echo "preg_replace example\n";

echo "<pre>";print_r(preg_replace($pattern, $replace, $subject));

?>

 

Output

 regular4.jpg

For more examples related to regular expression please visit the http://www.php.net/ website.  


Similar Articles