2. DEFINITION
• Regexes (regex) are sequences of characters describing special
search patterns as text strings.
• The basic purpose of them is to match loosely defined patterns to
accomplish some tasks in the programming world.
• Based on a pattern definition, the regex retrieves the strings based
on the exact sequence of characters.
3. ADVANTAGES
• It helps in important user information validations like email address,
phone numbers and IP address.
• It helps in identifying specific template tags and replacing those
data with the actual data as per the requirement.
• It helps in searching specific string pattern and extracting matching
results in a flexible manner.
• It offers a powerful tool for analyzing, searching a pattern and
modifying the text data.
4. OPERATORS
^ It denotes the start of string.
$ It denotes the end of string.
. It denotes almost any single
character.
() It denotes a group of
expressions.
[] It finds a range of characters for
example [xyz] means x, y or z .
5. [^] It finds the items which are not in
range for example [^abc] means
NOT a, b or c.
– (dash) It finds for character range within
the given item range for example
[a-z] means a through z.
| (pipe) It is the logical OR for example x |
y means x OR y.
? It denotes zero or one of
preceding character or item
range.
6. * It denotes zero or more of
preceding character or item
range.
+ It denotes one or more of
preceding character or item
range.
{n} It denotes exactly n times of
preceding character or item range
for example n{2}.
{n, m} It denotes atleast n but not more
than m times for example n{2, 4}
means 2 to 4 of n.
8. Preg_match_all () -> This function searches for a specific pattern
against some string. It returns true if pattern exists and false otherwise.
Eg.,
<?php (or) <?php
$exp = “ Cap and Map”; $exp = “ Bananas”;
$new= “api”; $new = “/pBa(na){2}/i”;
echo “preg_match_all($new, $exp)”; echo “preg_match_all($new,$exp)”;
?> ?>
9. <?php
$pattern = "/ca[kf]e/";
$text = "He was eating cake in the cafe.";
$matches = preg_match_all($pattern, $text, $array); echo $matches . "
matches were found.";
?>
O/P: 2 matches were found.
11. preg_match_all() -> This function searches for all the occurrences of string
pattern against the string. This function is very useful for search and replace.
Eg.,
<?php
$exp = ‘/^[a-z A-Z]*$/’;
$myname = “Kumar”;
if(preg_match($exp,$myname)); {
echo “ Match found”;
}
else {
echo “ Match not found”; }
?>
12. preg_replace() ->This function behaves like ereg_replace() function
provided the regular expressions can be used in the pattern and replacement
strings.
Eg.,
<?php
$exp = “([0-9]+)”;
$car = “ i brought thuis BMW car on 2020”;
$replace = “2022”;
$car = preg_replace($exp, $replace, $car);
echo $car;
?>
13. preg_split() -> The function behaves like the PHP split() function. It
splits the string by regular expressions as its parameters.
<?
$IP = "123.456.789.94";
$exp = "/./";
$new = preg_split($exp, $IP);
echo "$new[0] <br>";
echo "$new[1] <br>";
echo "$new[2] <br>";
echo "$new[3] <br>";
?>