Regular expression

What is a Regular Expressions?

Regular expressions are powerful pattern matching algorithm that can be performed in a single expression.

Regular expressions use arithmetic operators such as (+,-,^) to create complex expressions.

Regular expressions help you accomplish tasks such as validating email addresses, IP address etc.

Why to use regular expressions

  • Regular expressions simplify identifying patterns in string data by calling a single function. This saves us coding time.
  • When validating user input such as email address, domain names, telephone numbers, IP addresses,
  • Highlighting keywords in search results
  • When creating a custom HTML template. Regular expressions can be used to identify the template tags and replace them with actual data.

Regular expressions in PHP

PHP has built in functions that allow us to work with regular functions. Let’s now look at the commonly used regular expression functions in PHP.

  • preg_match – this function is used to perform a pattern match on a string. It returns true if a match is found and false if a match is not found.
  • preg_split – this function is used to perform a pattern match on a string and then split the results into a numeric array
  • preg_replace – this function is used to perform a pattern match on a string and then replace the match with the specified text.

Below is the syntax for a regular expression function such as preg_match,preg_split or preg_replace.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

<?php
$my_url = "www.guru99.com";
if (preg_match("/guru/", $my_url))
{
	echo "the url $my_url contains guru";
}
else
{
	echo "the url $my_url does not contain guru";
}
?>

  Browse to the URL http://localhost/phptuts/preg_match_simple.php

PHP Regular Expressions

<?php

$my_text="I Love Regular Expressions";

$my_array  = preg_split("/ /", $my_text);

print_r($my_array );

?>

  Browse to the URL http://localhost/phptuts/preg_split.php

PHP Regular Expressions

Explaining the pattern “[/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/]”

HERE,

  • “‘/…/'” starts and ends the regular expression
  • “^[a-zA-Z0-9._-]” matches any lower or upper case letters, numbers between 0 and 9 and dots, underscores or dashes.
  • “+@[a-zA-Z0-9-]” matches the @ symbol followed by lower or upper case letters, numbers between 0 and 9 or dashes.
  • “+\.[a-zA-Z.]{2,5}$/” escapes the dot using the backslash then matches any lower or upper case letters with a character length between 2 and 5 at the end of the string.

Browse to the URL http://localhost/phptuts/preg_match.php

PHP Regular Expressions

As you can see from the above example breakdown, metacharacters are very powerful when it comes to matching patterns.

Summary

  • A regular expression is a pattern match algorithm
  • Regular expressions are very useful when performing validation checks, creating HTML template systems that recognize tags etc.
  • PHP has built in functions namely preg_match,preg_split and preg_replace that support regular expressions.
  • Metacharacters allow us to create complex patterns
top
X
Welcome to Our Website
Welcome to WPBot
wpChatIcon