{"id":179,"date":"2018-07-12T11:36:42","date_gmt":"2018-07-12T11:36:42","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=179"},"modified":"2018-08-10T08:14:49","modified_gmt":"2018-08-10T08:14:49","slug":"regular-expressions-in-php","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/regular-expressions-in-php\/","title":{"rendered":"Regular Expressions in PHP"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Introduction:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Regular expressions are used to search for matching pattern in a string. The regular expression generally used to validate the data which is user entered in web form.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Regular expression are originating from Unix where grep statement is used to work with strings and manipulate text. By using few basic rules for regular expression, very complex search pattern can be created.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">PHP provides functions specific to two set of regular expression. These functions are specific to a particular set of regular expression. User can use any one as per his\/her convenience. These two types are<\/p>\r\n\r\n<ul>\r\n \t<li>POSIX Regular Expression (Portable Operating System Interface for uniX)<\/li>\r\n \t<li>PCRE (PERL Compatible Regular Expression)<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nRegular expression is also known as regex or regexp.\r\n\r\n&nbsp;\r\n\r\nPOSIX style functions for regular expression is start with ereg_.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Both the types are compiled into PHP by default. PCRE regular expressions are binary safe. Since version PHP5.3 POSIX style regular expression is deprecated. PCRE style functions for regular expression is start with preg_.<\/p>\r\n&nbsp;\r\n\r\nThe preg_match function is used to match a regular expression in a string.\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 102px\" border=\"1\" width=\"756\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 208.063px\"><strong>Function<\/strong><\/td>\r\n<td style=\"width: 519.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 208.063px\" rowspan=\"2\">preg_match($pattern, $string)\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 519.063px\" rowspan=\"2\">Search $pattern in $string. If matching found, the function\u00a0 will return 1 else\r\n\r\nit will return 0. In case, there is an error in $pattern it will return FALSE.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nIn PHP, each regex pattern is defined as text string and the pattern must be enclosed in forward slashes (\/).\r\n\r\n&nbsp;\r\n\r\nLet us write one example to understand preg_match function.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$pattern = '\/Hiren\/';\r\n\r\n$author = 'Hiren Joshi';\r\n\r\n$editor = 'Hardik Joshi';\r\n\r\n\/\/preg_match\r\n\r\n$ans = preg_match($pattern,$author);\r\n\r\necho $ans;\r\n\r\nif($ans === False)\r\n\r\n{\r\n\r\necho \"&lt;br&gt; There is error in pattern\";\r\n\r\n}\r\n\r\nelse if($ans === 1)\r\n\r\n{\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; pattern $pattern matched with author $author\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; pattern $pattern is not matched with author $author\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/editor check<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result = preg_match($pattern,$editor);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if($result === 1)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; pattern $pattern is matched with editor $editor\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else if($result === 0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; pattern $pattern is not matched with editor $editor\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; There is an error\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThen the output will be:\r\n\r\n&nbsp;\r\n\r\n1\r\n\r\n&nbsp;\r\n\r\npattern \/Hiren\/ matched with author Hiren Joshi\r\n\r\n&nbsp;\r\n\r\npattern \/Hiren\/ is not matched with editor Hardik Joshi\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To create case-insensitive regular expression use i at the end of regular expression. So the case is ignored in pattern and string.<\/p>\r\n&nbsp;\r\n\r\nFor example.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$pattern = '\/hiren\/';\r\n\r\n$author = 'HIREN JOSHI';\r\n\r\n\/\/preg_match\r\n\r\n$ans = preg_match($pattern,$author); \/\/ $ans is 0\r\n\r\necho \"&lt;br&gt; $ans\";\r\n\r\n\/\/case in-sensitive\r\n\r\n$pattern = '\/hiren\/i';\r\n\r\n$author = 'HIREN JOSHI';\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">\/\/preg_match<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans = preg_match($pattern,$author); \/\/ $ans is 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; $ans\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Character Matching<\/strong>\r\n<ol>\r\n \t<li style=\"text-align: justify\">Escape character in regular expression is backslash (\\). The back slash gives special meaning to some characters and removes special meaning from other characters.<\/li>\r\n \t<li style=\"text-align: justify\">If you want to match \\ in a string, you have to code \\\\\\\\. The reason is that as \\ is an escape character, \\\\ interpreted as \\ which is again treated as an escape character. So you have to code \\\\\\\\to match a single \\.<\/li>\r\n \t<li style=\"text-align: justify\">To match forward slash you have to code \\\/ (back slash forward slash) because pattern is enclosed Between forward slashes.<\/li>\r\n \t<li style=\"text-align: justify\">The character is known as metacharacter if it has a special meaning in a pattern. For example, back slash, forward slash are example of metacharacter.<\/li>\r\n \t<li style=\"text-align: justify\">It is advisable to use single quote for regular expression. Though double quote can be used for regular expression, it creates unnecessary complications.<\/li>\r\n \t<li>Following code snippet shows the code to match back slash.<\/li>\r\n<\/ol>\r\n&nbsp;\r\n\r\n$string = 'This is backslash \\ which is used as escape character in regex';\r\n\r\n$ans = preg_match('\/\\\\\\\\\/',$string); \/\/ will return 1\r\n\r\n$string = 'This is forwardslash \/ which is used in regex';\r\n\r\n$ans = preg_match('\/\\\/\/',$string); \/\/ will return 1\r\n\r\n&nbsp;\r\n\r\nFollowing table shows few match types of characters.\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 293px\" border=\"1\" width=\"635\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 57.0625px\"><strong>Pattern<\/strong><\/td>\r\n<td style=\"width: 549.063px\"><strong>Matches<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 57.0625px\">.<\/td>\r\n<td style=\"width: 549.063px\">The dot matches any single character. Use \\. (back slash dot) to match . (dot)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 57.0625px\">\\w<\/td>\r\n<td style=\"width: 549.063px\">Matches any upper or lower alphabet , digit or\u00a0 _<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 57.0625px\">\\W<\/td>\r\n<td style=\"width: 549.063px\">Matches any non alpha-numeric character excluding _<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 57.0625px\">\\d<\/td>\r\n<td style=\"width: 549.063px\">Any digit character<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 57.0625px\">\\D<\/td>\r\n<td style=\"width: 549.063px\">Any non-digit character<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 57.0625px\">\\s<\/td>\r\n<td style=\"width: 549.063px\">Any whitespace character<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 57.0625px\">\\S<\/td>\r\n<td style=\"width: 549.063px\">Any non-white space character<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nFollowing code snippet shows the example of above pattern matching\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$string = 'The product code is JTK-4702';\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans1 = preg_match('\/JT.\/',$string);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Answer : $ans1\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans2 = preg_match('\/JT\\d\/',$string);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Answer : $ans2\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans3 = preg_match('\/JTK-\\d\/',$string);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Answer : $ans3\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThen the output will be:\r\n\r\n&nbsp;\r\n\r\nAnswer : 1\r\n\r\nAnswer : 0\r\n\r\nAnswer : 1\r\n\r\n&nbsp;\r\n\r\n<strong>Character Class<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The character class allows you to match a single character from the set of characters. The characters between square bracket( [ ]) is matched with the character. For example, [xyz] matches with x or y or z. To match opening square bracket you can use \\[. In the same way \\] is used to match closing square bracket.<\/p>\r\n&nbsp;\r\n\r\n$string = 'The product code is JTK-4702';\r\n\r\n$ans1 = preg_match('\/JT[JKL]\/',$string);\u00a0 \/\/ returns 1\r\n\r\n$ans2 = preg_match('\/JTK-[1234]\/',$string);\u00a0 \/\/ returns 1\r\n\r\n$ans3 = $ans3 = preg_match('\/JTK-[123]\/',$string); \/\/ returns 0\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Most of the metacharcaters loses their special meaning inside a character class. However, ^ and \u2013 is retain their special meaning inside a character class and these characters are position dependent.<\/p>\r\n&nbsp;\r\n\r\n<strong>Metacharacters inside a character class<\/strong>\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 69.0625px\"><strong>Character<\/strong><\/td>\r\n<td style=\"width: 59.0625px\"><strong>Example<\/strong><\/td>\r\n<td style=\"width: 379.063px\"><strong>Meaning<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 69.0625px\" rowspan=\"3\">^\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 59.0625px\" rowspan=\"3\">[^aeoiu]\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 379.063px\" rowspan=\"3\">^ is treated as not. So it negate the set of characters\r\n\r\ninside a character class. Matches any character except\r\n\r\na,e,I,o or u.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 69.0625px\" rowspan=\"3\">-\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 59.0625px\" rowspan=\"3\">[a-z]\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 379.063px\" rowspan=\"3\">-\u00a0\u00a0\u00a0 (read as hyphen ) is used to represent range.\r\n\r\nIt matches any small alphabet character\r\n\r\nbetween a to z.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Below are few examples of metacharacter inside a character class.<\/strong><\/p>\r\n&nbsp;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">\u00a0$string = 'The product code is JTK-4702';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0$ans1 = preg_match('\/JT[^JKL]\/',$string);\u00a0 \/\/ returns 0<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0$ans2 = preg_match('\/JTK[^^]\/',$string);\u00a0 \/\/ returns 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0$ans3 = preg_match('\/JTK-[1-5]\/',$string); \/\/ returns 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0$ans4 = preg_match('\/JTK[*-_]\/',$string);\u00a0 \/\/ returns 1<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Bracket Expression<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Bracket expression can be used in character class. Bracket expression is a named range of characters. For example [:digit:] expression is equivalent to the range 0-9. Following table lists bracket expression used in a character class.<\/p>\r\n\r\n<table class=\"aligncenter\" style=\"height: 329px\" border=\"1\" width=\"572\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 63.0625px\"><strong>Pattern<\/strong><\/td>\r\n<td style=\"width: 465.063px\"><strong>Matches<\/strong><\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 63.0625px\">[:digit:]<\/td>\r\n<td style=\"width: 465.063px\">Digits (same as \\d)<\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 63.0625px\">[:lower:]<\/td>\r\n<td style=\"width: 465.063px\">Lower case letters<\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 63.0625px\">[:upper:]<\/td>\r\n<td style=\"width: 465.063px\">Upper case letters<\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 63.0625px\">[:letter:]<\/td>\r\n<td style=\"width: 465.063px\">Lower and upper case letters<\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 63.0625px\">[:alnum:]<\/td>\r\n<td style=\"width: 465.063px\">Alphanumeric (upper and lower case letters and digits)<\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 63.0625px\">[:word:]<\/td>\r\n<td style=\"width: 465.063px\">Upper and lower case letters, digits and underscores (same as \\w)<\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 63.0625px\">[:print:]<\/td>\r\n<td style=\"width: 465.063px\">All printable characters including the space<\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 63.0625px\">[:graph:]<\/td>\r\n<td style=\"width: 465.063px\">All printable characters excluding the space<\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 63.0625px\">[:punct:]<\/td>\r\n<td style=\"width: 465.063px\">All printable character excluding letters and digits<\/td>\r\n<td style=\"width: 1.0625px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nExample for bracket expression\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$string = 'The product code is JTK-4702';\r\n\r\n$ans1 = preg_match('\/JTK[[:punct:]]\/',$string); \/\/ return 1\r\n\r\n$ans2 = preg_match('\/JT[[:upper:]]\/',$string); \/\/ return 1\r\n\r\n$ans3 = preg_match('\/JTK[[:digit:]]\/',$string); \/\/ return 0\r\n\r\n$ans4 = preg_match('\/JTK-[[:digit:]]\/',$string); \/\/ return 1\r\n\r\n?&gt;\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">Match string position<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Positional indicator and subpattern repletion allows developer to write complex patterns. The following table list positional indicators.<\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Pattern<\/strong><\/td>\r\n<td><strong>Matches<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>^<\/td>\r\n<td>Starting of the string.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>$<\/td>\r\n<td>Ending of the string<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>^pattern$<\/td>\r\n<td>The entire string must match<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>\\b<\/td>\r\n<td>The starting or ending of the word<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>\\B<\/td>\r\n<td>A position other than starting or ending of a word<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Example for matching string position<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$string = 'The product code is JTK-4702';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans1 = preg_match('\/^The\/',$string);\u00a0\u00a0 \/\/ return 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans2 = preg_match('\/4702$\/',$string); \/\/ return 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans3 = preg_match('\/code\/',$string);\u00a0\u00a0 \/\/ return 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans4 = preg_match('\/^The product code is JTK-4702$\/',$string);\u00a0 \u00a0\/\/ return 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans5 = preg_match('\/JTK\\b\/',$string);\u00a0 \/\/ return 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans6 = preg_match('\/rod\\B\/',$string); \/\/ return 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">Group and match subpattern<\/strong>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">A part of pattern can be grouped into subpattern. Numbered and unnumbered subpattern can be created by using parenthesis.<\/span>\r\n<div>\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Pattern<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>(subpattern)<\/td>\r\n<td>Creates a numbered subpattern group<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>(?:subpattern)<\/td>\r\n<td>Creates an unnumbered subpattern group<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>|<\/td>\r\n<td>Matches either left side or right side subpattern<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>\\n<\/td>\r\n<td>Matches a numbered subpattern<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nFor example,\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$string = 'The product code is JTK-4702';\r\n\r\n$string1 = 'Raj Rajeshwar';\r\n\r\n$ans1 = preg_match('\/^(Raj)|(Kaj)\/',$string1);\r\n\r\necho \"&lt;br&gt; Answer1 : $ans1\";\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans2 = preg_match('\/^(\\w\\w\\w) \\1\/',$string1); \/\/ numbered subpattern echo \"&lt;br&gt; Answer2 : $ans2\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans3 = preg_match('\/^(The)|(That)\/',$string);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Answer3 : $ans3\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans4 = preg_match('\/(rod\\B)|(That)\/',$string);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Answer4 : $ans4\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/ matching ode<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$pattern = '\/(?:ode)\/';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans5 = preg_match($pattern,$string);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if($ans5 === 1)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; $pattern is matched with $string\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else if($ans5 === 0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; $pattern is not matched with $string\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">else<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Error\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe output will be:\r\n\r\n&nbsp;\r\n\r\nAnswer1 : 1\r\n\r\nAnswer2 : 1\r\n\r\nAnswer3 : 1\r\n\r\nAnswer4 : 1\r\n\r\n&nbsp;\r\n\r\n\/(?:ode)\/ is matched with The product code is JTK-4702\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">Matching a repeating pattern<\/strong>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">To match\u00a0 a repeating\u00a0 pattern following patterns are used.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Pattern<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>{n}<\/td>\r\n<td>Pattern must be\u00a0 repeated exactly n times.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>{n, }<\/td>\r\n<td>Pattern must be repeated\u00a0 n or more times.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>{n, m}<\/td>\r\n<td>Pattern must be\u00a0 repeated from minimum n times to maximum m times<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>?<\/td>\r\n<td>Zero or one of the previous subpattern. Same as {0,1}<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>+<\/td>\r\n<td>One or more of the previous subpattern. Same as {1,}<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>*<\/td>\r\n<td>Zero or more of the previous subpattern. Same as {0,}<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$phone = '123-456-7890';\r\n\r\n$phonepattern = '\/^\\d{3}-\\d{3}-\\d{4}$\/';\r\n\r\n$matchphonepattern = preg_match($phonepattern,$phone); if($matchphonepattern === 1) {\r\n\r\necho \"&lt;br&gt; Phone $phone is matched\";\r\n\r\n}\r\n\r\n$fax = '(123) 456-7890';\r\n\r\n$faxpattern = '\/^\\(\\d{3}\\) \\d{3}-\\d{4}$\/';\r\n\r\n$matchfaxpattern = preg_match($faxpattern,$fax);\r\n\r\nif($matchfaxpattern === 1)\r\n\r\n{\r\n\r\necho \"&lt;br&gt; Fax $fax is matched\";\r\n\r\n}\r\n\r\n\/\/ phone or fax\r\n\r\n$phoneorfax = '\/^(\\(\\d{3}\\) |(\\d{3}-) ?)\\d{3}-\\d{4}$\/'; $pmatch = preg_match($phoneorfax, $phone); if($pmatch === 1)\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Phone is matched with phone or fax\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$fmatch = preg_match($phoneorfax, $fax);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if($fmatch === 1)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Fax is matched with phone or fax\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThen the output is:\r\n\r\n&nbsp;\r\n\r\nPhone 123-456-7890 is matched\r\n\r\nFax (123) 456-7890 is matched\r\n\r\nPhone is matched with phone or fax\r\n\r\nFax is matched with phone or fax\r\n\r\n&nbsp;\r\n\r\n<strong>Look-ahead assertion<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A look ahead assertion is a special type of subpattern. The look-ahead assertion is an assertion that must be matched for the overall pattern to me matched. A look ahead assertion must be matched starting immediately after the position of the assertion in the pattern. A negative look-ahead assertion is an assertion which does not matched for the overall pattern to be matched.<\/p>\r\n&nbsp;\r\n\r\nTo create a look ahead assertion, code ? followed by = between parenthesises .\r\n\r\n&nbsp;\r\n\r\nLook-ahead assertion (?=assertion) while negative look-ahead assertion is (!?assertion).\r\n\r\n&nbsp;\r\n\r\nThe look-ahead assertion (?=[[:digit:]]) is used to match the next character in the pattern must be a digit.\r\n\r\n&nbsp;\r\n\r\nThe look-ahead assertion (?=.*[[:digit:]]) is used to match that the pattern must contain at-least one digit.\r\n\r\n&nbsp;\r\n\r\nFollowing code show another example of look-ahead assertion.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$pattern1 = '\/^(?=.*[[:digit:]])[[:alnum:]]{6}$\/';\r\n\r\n$ans1 = preg_match($pattern1,'Paresh');\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Ans1 : $ans1\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans2 = preg_match($pattern1,'Pares8');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Ans2 : $ans2\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/negative lookahead assertion<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$pattern2 = '\/^(?!3[2-9])[0-3][[:digit:]]$\/';\u00a0 \/\/ values excluded between 32 to 39. Negative look-ahead<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans3 = preg_match($pattern2,'32');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Ans3 : $ans3\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$ans4 = preg_match($pattern2,'31');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Ans4 : $ans4\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/password complexity<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$pwd = '\/^(?=.*[[:digit:]])(?=.*[[:punct:]])[[:print:]]{6,}$\/';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 ^\u00a0 represent from the start of the pattern<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 (?=.*[[:digit:]]) represent that the assertion must contain atleast one digit<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 \u00a0(?=.*[[:punct:]]) represent that the assertion must containt atleast one punctuation character<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 \u00a0[[:print:]]{6,} represent that the pattern must have 6 or more printable characters<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0\u00a0\u00a0 $ represent from the end of the pattern<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$pass1 = 'super3man';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$p1 = preg_match($pwd, $pass1);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; pass1 : $p1\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$pass2 = 'supe2m@n';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$p2 = preg_match($pwd, $pass2);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; pass2 : $p2\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe output :\r\n\r\n&nbsp;\r\n\r\nAns1 : 0\r\n\r\nAns2 : 1\r\n\r\nAns3 : 0\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Ans4 : 1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">pass1 : 0<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">pass2 : 1<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Global Regular Expression<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A global regular expression is used to find multiple matches of a pattern in a string. To find multiple matches of a pattern in a string, preg_match_all function is used.<\/p>\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 151px\" border=\"1\" width=\"734\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 233.063px\"><strong>Function<\/strong><\/td>\r\n<td style=\"width: 472.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 233.063px\" rowspan=\"4\">preg_match_all($pattern,$string,\r\n\r\n$matches)\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 472.063px\" rowspan=\"4\">Returns no. of matches found in a string. This function stores all the\r\n\r\nmatched substrings in $matches. $matches is a multi-dimensional\r\n\r\narray. In $matches, $matches[0] (the first element) is an array of\r\n\r\nmatched substring.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$string = 'JTK-4702 JTK-1379';\r\n\r\n$pattern = '\/JTK-[[:digit:]]{4}\/';\r\n\r\n$count = preg_match_all($pattern,$string,$matches);\r\n\r\nforeach($matches[0] as $match)\r\n\r\n{\r\n\r\necho \"&lt;br&gt; $match\";\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nThe output is:\r\n\r\n&nbsp;\r\n\r\nJTK-4702\r\n\r\nJTK-1379\r\n\r\n&nbsp;\r\n\r\n<strong>Replace a regular expression with string<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To replace the text which matches a pattern, preg_replace function is used. This function works similar to str_replace and stri_replace. However, preg_replace function allows to replace any text which can be matched with specified regular expression.<\/p>\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Function<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">preg_replace($pattern,$new,\r\n\r\n$string)<\/td>\r\n<td rowspan=\"2\">Returns a string which is created by\u00a0 replacing the $new of $string.\r\n\r\n&nbsp;<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$items = 'JTK-4702 JTM-3618';\r\n\r\n$items = preg_replace('\/JT[KM]\/','PCode',$items);\r\n\r\necho $items;\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nThe output is:\r\n\r\n&nbsp;\r\n\r\nPCode-4702 PCode-3618\r\n\r\n&nbsp;\r\n\r\n<strong>Following code shows few of data validation using regular expression<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/phone in 999-999-9999 format\r\n\r\n$phone = '\/^\\d{3}-\\d{3}-\\d{4}$\/';\r\n\r\n$phone1 = '\/^[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$\/'; $p1 ='559-237-8024';\r\n\r\n$ans1 = preg_match($phone,$p1);\r\n\r\necho \"&lt;br&gt; $ans1\";\r\n\r\n$ans11 = preg_match($phone1,$p1);\r\n\r\necho \"&lt;br&gt; Ans11 : $ans11\";\r\n\r\n\/\/\u00a0 Credit Card in 9999-9999-9999-9999-999 $creditcard = '\/^\\d{4}(-\\d{4}){3}$\/'; $card = '1234-5678-9012-3456';\r\n\r\n$cardmatch = preg_match($creditcard,$card); echo \"&lt;br&gt; Card Match is : $cardmatch\"; \/\/Code in 99999 or 99999-9999\r\n\r\n$code = '\/^\\d{5}(-\\d{4}) ?$\/'; $zip1 = '12345';\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$zipm1 = preg_match($code,$zip1);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Zip1 $zip1 is matched\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$zip2 = '12345-6789';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$zipm2 = preg_match($code,$zip2);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Zip2 $zip2 is matched\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/date in format dd\/mm\/yyyy<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$date = '\/^(0?[1-9]|[12][[:digit:]]|[3][01])\\\/(0?[1-9]|1[012])\\\/[[:digit:]]{4}$\/'; $bday = '12\/6\/1985';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$bdmatch = preg_match($date, $bday);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Birthday : $bdmatch\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong>The output will be:<\/strong>\r\n\r\n&nbsp;\r\n\r\n1\r\n\r\nAns11 : 1\r\n\r\nCard Match is : 1\r\n\r\nZip1 12345 is matched\r\n\r\nZip2 12345-6789 is matched\r\n\r\nBirthday : 1\r\n\r\n&nbsp;\r\n\r\n<strong>References and suggested additional reading:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\r\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\r\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\r\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O'Reilly Media<\/p>\r\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP 5, Wrox<\/p>\r\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\r\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\r\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\r\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\r\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education<\/p>\r\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\r\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\r\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\r\n<p style=\"text-align: justify\">14. http:\/\/www.noupe.com\/development\/php-regular-expressions.html<\/p>\r\n<p style=\"text-align: justify\">15. http:\/\/php5.kiev.ua\/manual\/en\/regexp.reference.subpatterns.html<\/p>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p><strong>Introduction:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Regular expressions are used to search for matching pattern in a string. The regular expression generally used to validate the data which is user entered in web form.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Regular expression are originating from Unix where grep statement is used to work with strings and manipulate text. By using few basic rules for regular expression, very complex search pattern can be created.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">PHP provides functions specific to two set of regular expression. These functions are specific to a particular set of regular expression. User can use any one as per his\/her convenience. These two types are<\/p>\n<ul>\n<li>POSIX Regular Expression (Portable Operating System Interface for uniX)<\/li>\n<li>PCRE (PERL Compatible Regular Expression)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Regular expression is also known as regex or regexp.<\/p>\n<p>&nbsp;<\/p>\n<p>POSIX style functions for regular expression is start with ereg_.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Both the types are compiled into PHP by default. PCRE regular expressions are binary safe. Since version PHP5.3 POSIX style regular expression is deprecated. PCRE style functions for regular expression is start with preg_.<\/p>\n<p>&nbsp;<\/p>\n<p>The preg_match function is used to match a regular expression in a string.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 102px; width: 756px;\">\n<tbody>\n<tr>\n<td style=\"width: 208.063px\"><strong>Function<\/strong><\/td>\n<td style=\"width: 519.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 208.063px\" rowspan=\"2\">preg_match($pattern, $string)<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 519.063px\" rowspan=\"2\">Search $pattern in $string. If matching found, the function\u00a0 will return 1 else<\/p>\n<p>it will return 0. In case, there is an error in $pattern it will return FALSE.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>In PHP, each regex pattern is defined as text string and the pattern must be enclosed in forward slashes (\/).<\/p>\n<p>&nbsp;<\/p>\n<p>Let us write one example to understand preg_match function.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$pattern = &#8216;\/Hiren\/&#8217;;<\/p>\n<p>$author = &#8216;Hiren Joshi&#8217;;<\/p>\n<p>$editor = &#8216;Hardik Joshi&#8217;;<\/p>\n<p>\/\/preg_match<\/p>\n<p>$ans = preg_match($pattern,$author);<\/p>\n<p>echo $ans;<\/p>\n<p>if($ans === False)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; There is error in pattern&#8221;;<\/p>\n<p>}<\/p>\n<p>else if($ans === 1)<\/p>\n<p>{<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; pattern $pattern matched with author $author&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; pattern $pattern is not matched with author $author&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/editor check<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result = preg_match($pattern,$editor);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if($result === 1)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; pattern $pattern is matched with editor $editor&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else if($result === 0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; pattern $pattern is not matched with editor $editor&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; There is an error&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Then the output will be:<\/p>\n<p>&nbsp;<\/p>\n<p>1<\/p>\n<p>&nbsp;<\/p>\n<p>pattern \/Hiren\/ matched with author Hiren Joshi<\/p>\n<p>&nbsp;<\/p>\n<p>pattern \/Hiren\/ is not matched with editor Hardik Joshi<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To create case-insensitive regular expression use i at the end of regular expression. So the case is ignored in pattern and string.<\/p>\n<p>&nbsp;<\/p>\n<p>For example.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$pattern = &#8216;\/hiren\/&#8217;;<\/p>\n<p>$author = &#8216;HIREN JOSHI&#8217;;<\/p>\n<p>\/\/preg_match<\/p>\n<p>$ans = preg_match($pattern,$author); \/\/ $ans is 0<\/p>\n<p>echo &#8220;&lt;br&gt; $ans&#8221;;<\/p>\n<p>\/\/case in-sensitive<\/p>\n<p>$pattern = &#8216;\/hiren\/i&#8217;;<\/p>\n<p>$author = &#8216;HIREN JOSHI&#8217;;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">\/\/preg_match<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans = preg_match($pattern,$author); \/\/ $ans is 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; $ans&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Character Matching<\/strong><\/p>\n<ol>\n<li style=\"text-align: justify\">Escape character in regular expression is backslash (\\). The back slash gives special meaning to some characters and removes special meaning from other characters.<\/li>\n<li style=\"text-align: justify\">If you want to match \\ in a string, you have to code \\\\\\\\. The reason is that as \\ is an escape character, \\\\ interpreted as \\ which is again treated as an escape character. So you have to code \\\\\\\\to match a single \\.<\/li>\n<li style=\"text-align: justify\">To match forward slash you have to code \\\/ (back slash forward slash) because pattern is enclosed Between forward slashes.<\/li>\n<li style=\"text-align: justify\">The character is known as metacharacter if it has a special meaning in a pattern. For example, back slash, forward slash are example of metacharacter.<\/li>\n<li style=\"text-align: justify\">It is advisable to use single quote for regular expression. Though double quote can be used for regular expression, it creates unnecessary complications.<\/li>\n<li>Following code snippet shows the code to match back slash.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>$string = &#8216;This is backslash \\ which is used as escape character in regex&#8217;;<\/p>\n<p>$ans = preg_match(&#8216;\/\\\\\\\\\/&#8217;,$string); \/\/ will return 1<\/p>\n<p>$string = &#8216;This is forwardslash \/ which is used in regex&#8217;;<\/p>\n<p>$ans = preg_match(&#8216;\/\\\/\/&#8217;,$string); \/\/ will return 1<\/p>\n<p>&nbsp;<\/p>\n<p>Following table shows few match types of characters.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 293px; width: 635px;\">\n<tbody>\n<tr>\n<td style=\"width: 57.0625px\"><strong>Pattern<\/strong><\/td>\n<td style=\"width: 549.063px\"><strong>Matches<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 57.0625px\">.<\/td>\n<td style=\"width: 549.063px\">The dot matches any single character. Use \\. (back slash dot) to match . (dot)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 57.0625px\">\\w<\/td>\n<td style=\"width: 549.063px\">Matches any upper or lower alphabet , digit or\u00a0 _<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 57.0625px\">\\W<\/td>\n<td style=\"width: 549.063px\">Matches any non alpha-numeric character excluding _<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 57.0625px\">\\d<\/td>\n<td style=\"width: 549.063px\">Any digit character<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 57.0625px\">\\D<\/td>\n<td style=\"width: 549.063px\">Any non-digit character<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 57.0625px\">\\s<\/td>\n<td style=\"width: 549.063px\">Any whitespace character<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 57.0625px\">\\S<\/td>\n<td style=\"width: 549.063px\">Any non-white space character<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Following code snippet shows the example of above pattern matching<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$string = &#8216;The product code is JTK-4702&#8217;;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans1 = preg_match(&#8216;\/JT.\/&#8217;,$string);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Answer : $ans1&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans2 = preg_match(&#8216;\/JT\\d\/&#8217;,$string);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Answer : $ans2&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans3 = preg_match(&#8216;\/JTK-\\d\/&#8217;,$string);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Answer : $ans3&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Then the output will be:<\/p>\n<p>&nbsp;<\/p>\n<p>Answer : 1<\/p>\n<p>Answer : 0<\/p>\n<p>Answer : 1<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Character Class<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The character class allows you to match a single character from the set of characters. The characters between square bracket( [ ]) is matched with the character. For example, [xyz] matches with x or y or z. To match opening square bracket you can use \\[. In the same way \\] is used to match closing square bracket.<\/p>\n<p>&nbsp;<\/p>\n<p>$string = &#8216;The product code is JTK-4702&#8217;;<\/p>\n<p>$ans1 = preg_match(&#8216;\/JT[JKL]\/&#8217;,$string);\u00a0 \/\/ returns 1<\/p>\n<p>$ans2 = preg_match(&#8216;\/JTK-[1234]\/&#8217;,$string);\u00a0 \/\/ returns 1<\/p>\n<p>$ans3 = $ans3 = preg_match(&#8216;\/JTK-[123]\/&#8217;,$string); \/\/ returns 0<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Most of the metacharcaters loses their special meaning inside a character class. However, ^ and \u2013 is retain their special meaning inside a character class and these characters are position dependent.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Metacharacters inside a character class<\/strong><\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 69.0625px\"><strong>Character<\/strong><\/td>\n<td style=\"width: 59.0625px\"><strong>Example<\/strong><\/td>\n<td style=\"width: 379.063px\"><strong>Meaning<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 69.0625px\" rowspan=\"3\">^<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 59.0625px\" rowspan=\"3\">[^aeoiu]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 379.063px\" rowspan=\"3\">^ is treated as not. So it negate the set of characters<\/p>\n<p>inside a character class. Matches any character except<\/p>\n<p>a,e,I,o or u.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 69.0625px\" rowspan=\"3\">&#8211;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 59.0625px\" rowspan=\"3\">[a-z]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 379.063px\" rowspan=\"3\">&#8211;\u00a0\u00a0\u00a0 (read as hyphen ) is used to represent range.<\/p>\n<p>It matches any small alphabet character<\/p>\n<p>between a to z.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Below are few examples of metacharacter inside a character class.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">\u00a0$string = &#8216;The product code is JTK-4702&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0$ans1 = preg_match(&#8216;\/JT[^JKL]\/&#8217;,$string);\u00a0 \/\/ returns 0<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0$ans2 = preg_match(&#8216;\/JTK[^^]\/&#8217;,$string);\u00a0 \/\/ returns 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0$ans3 = preg_match(&#8216;\/JTK-[1-5]\/&#8217;,$string); \/\/ returns 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0$ans4 = preg_match(&#8216;\/JTK[*-_]\/&#8217;,$string);\u00a0 \/\/ returns 1<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Bracket Expression<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Bracket expression can be used in character class. Bracket expression is a named range of characters. For example [:digit:] expression is equivalent to the range 0-9. Following table lists bracket expression used in a character class.<\/p>\n<table class=\"aligncenter\" style=\"height: 329px; width: 572px;\">\n<tbody>\n<tr>\n<td style=\"width: 63.0625px\"><strong>Pattern<\/strong><\/td>\n<td style=\"width: 465.063px\"><strong>Matches<\/strong><\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.0625px\">[:digit:]<\/td>\n<td style=\"width: 465.063px\">Digits (same as \\d)<\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.0625px\">[:lower:]<\/td>\n<td style=\"width: 465.063px\">Lower case letters<\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.0625px\">[:upper:]<\/td>\n<td style=\"width: 465.063px\">Upper case letters<\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.0625px\">[:letter:]<\/td>\n<td style=\"width: 465.063px\">Lower and upper case letters<\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.0625px\">[:alnum:]<\/td>\n<td style=\"width: 465.063px\">Alphanumeric (upper and lower case letters and digits)<\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.0625px\">[:word:]<\/td>\n<td style=\"width: 465.063px\">Upper and lower case letters, digits and underscores (same as \\w)<\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.0625px\">[:print:]<\/td>\n<td style=\"width: 465.063px\">All printable characters including the space<\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.0625px\">[:graph:]<\/td>\n<td style=\"width: 465.063px\">All printable characters excluding the space<\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.0625px\">[:punct:]<\/td>\n<td style=\"width: 465.063px\">All printable character excluding letters and digits<\/td>\n<td style=\"width: 1.0625px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Example for bracket expression<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$string = &#8216;The product code is JTK-4702&#8217;;<\/p>\n<p>$ans1 = preg_match(&#8216;\/JTK[[:punct:]]\/&#8217;,$string); \/\/ return 1<\/p>\n<p>$ans2 = preg_match(&#8216;\/JT[[:upper:]]\/&#8217;,$string); \/\/ return 1<\/p>\n<p>$ans3 = preg_match(&#8216;\/JTK[[:digit:]]\/&#8217;,$string); \/\/ return 0<\/p>\n<p>$ans4 = preg_match(&#8216;\/JTK-[[:digit:]]\/&#8217;,$string); \/\/ return 1<\/p>\n<p>?&gt;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">Match string position<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Positional indicator and subpattern repletion allows developer to write complex patterns. The following table list positional indicators.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Pattern<\/strong><\/td>\n<td><strong>Matches<\/strong><\/td>\n<\/tr>\n<tr>\n<td>^<\/td>\n<td>Starting of the string.<\/td>\n<\/tr>\n<tr>\n<td>$<\/td>\n<td>Ending of the string<\/td>\n<\/tr>\n<tr>\n<td>^pattern$<\/td>\n<td>The entire string must match<\/td>\n<\/tr>\n<tr>\n<td>\\b<\/td>\n<td>The starting or ending of the word<\/td>\n<\/tr>\n<tr>\n<td>\\B<\/td>\n<td>A position other than starting or ending of a word<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example for matching string position<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$string = &#8216;The product code is JTK-4702&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans1 = preg_match(&#8216;\/^The\/&#8217;,$string);\u00a0\u00a0 \/\/ return 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans2 = preg_match(&#8216;\/4702$\/&#8217;,$string); \/\/ return 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans3 = preg_match(&#8216;\/code\/&#8217;,$string);\u00a0\u00a0 \/\/ return 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans4 = preg_match(&#8216;\/^The product code is JTK-4702$\/&#8217;,$string);\u00a0 \u00a0\/\/ return 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans5 = preg_match(&#8216;\/JTK\\b\/&#8217;,$string);\u00a0 \/\/ return 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans6 = preg_match(&#8216;\/rod\\B\/&#8217;,$string); \/\/ return 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">Group and match subpattern<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">A part of pattern can be grouped into subpattern. Numbered and unnumbered subpattern can be created by using parenthesis.<\/span><\/p>\n<div>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Pattern<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>(subpattern)<\/td>\n<td>Creates a numbered subpattern group<\/td>\n<\/tr>\n<tr>\n<td>(?:subpattern)<\/td>\n<td>Creates an unnumbered subpattern group<\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td>Matches either left side or right side subpattern<\/td>\n<\/tr>\n<tr>\n<td>\\n<\/td>\n<td>Matches a numbered subpattern<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>For example,<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$string = &#8216;The product code is JTK-4702&#8217;;<\/p>\n<p>$string1 = &#8216;Raj Rajeshwar&#8217;;<\/p>\n<p>$ans1 = preg_match(&#8216;\/^(Raj)|(Kaj)\/&#8217;,$string1);<\/p>\n<p>echo &#8220;&lt;br&gt; Answer1 : $ans1&#8221;;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans2 = preg_match(&#8216;\/^(\\w\\w\\w) \\1\/&#8217;,$string1); \/\/ numbered subpattern echo &#8220;&lt;br&gt; Answer2 : $ans2&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans3 = preg_match(&#8216;\/^(The)|(That)\/&#8217;,$string);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Answer3 : $ans3&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans4 = preg_match(&#8216;\/(rod\\B)|(That)\/&#8217;,$string);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Answer4 : $ans4&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/ matching ode<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$pattern = &#8216;\/(?:ode)\/&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans5 = preg_match($pattern,$string);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if($ans5 === 1)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; $pattern is matched with $string&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else if($ans5 === 0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; $pattern is not matched with $string&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">else<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Error&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>The output will be:<\/p>\n<p>&nbsp;<\/p>\n<p>Answer1 : 1<\/p>\n<p>Answer2 : 1<\/p>\n<p>Answer3 : 1<\/p>\n<p>Answer4 : 1<\/p>\n<p>&nbsp;<\/p>\n<p>\/(?:ode)\/ is matched with The product code is JTK-4702<\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">Matching a repeating pattern<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">To match\u00a0 a repeating\u00a0 pattern following patterns are used.<\/span><\/p>\n<\/div>\n<div>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Pattern<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>{n}<\/td>\n<td>Pattern must be\u00a0 repeated exactly n times.<\/td>\n<\/tr>\n<tr>\n<td>{n, }<\/td>\n<td>Pattern must be repeated\u00a0 n or more times.<\/td>\n<\/tr>\n<tr>\n<td>{n, m}<\/td>\n<td>Pattern must be\u00a0 repeated from minimum n times to maximum m times<\/td>\n<\/tr>\n<tr>\n<td>?<\/td>\n<td>Zero or one of the previous subpattern. Same as {0,1}<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>One or more of the previous subpattern. Same as {1,}<\/td>\n<\/tr>\n<tr>\n<td>*<\/td>\n<td>Zero or more of the previous subpattern. Same as {0,}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$phone = &#8216;123-456-7890&#8217;;<\/p>\n<p>$phonepattern = &#8216;\/^\\d{3}-\\d{3}-\\d{4}$\/&#8217;;<\/p>\n<p>$matchphonepattern = preg_match($phonepattern,$phone); if($matchphonepattern === 1) {<\/p>\n<p>echo &#8220;&lt;br&gt; Phone $phone is matched&#8221;;<\/p>\n<p>}<\/p>\n<p>$fax = &#8216;(123) 456-7890&#8217;;<\/p>\n<p>$faxpattern = &#8216;\/^\\(\\d{3}\\) \\d{3}-\\d{4}$\/&#8217;;<\/p>\n<p>$matchfaxpattern = preg_match($faxpattern,$fax);<\/p>\n<p>if($matchfaxpattern === 1)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; Fax $fax is matched&#8221;;<\/p>\n<p>}<\/p>\n<p>\/\/ phone or fax<\/p>\n<p>$phoneorfax = &#8216;\/^(\\(\\d{3}\\) |(\\d{3}-) ?)\\d{3}-\\d{4}$\/&#8217;; $pmatch = preg_match($phoneorfax, $phone); if($pmatch === 1)<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Phone is matched with phone or fax&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$fmatch = preg_match($phoneorfax, $fax);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if($fmatch === 1)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Fax is matched with phone or fax&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Then the output is:<\/p>\n<p>&nbsp;<\/p>\n<p>Phone 123-456-7890 is matched<\/p>\n<p>Fax (123) 456-7890 is matched<\/p>\n<p>Phone is matched with phone or fax<\/p>\n<p>Fax is matched with phone or fax<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Look-ahead assertion<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A look ahead assertion is a special type of subpattern. The look-ahead assertion is an assertion that must be matched for the overall pattern to me matched. A look ahead assertion must be matched starting immediately after the position of the assertion in the pattern. A negative look-ahead assertion is an assertion which does not matched for the overall pattern to be matched.<\/p>\n<p>&nbsp;<\/p>\n<p>To create a look ahead assertion, code ? followed by = between parenthesises .<\/p>\n<p>&nbsp;<\/p>\n<p>Look-ahead assertion (?=assertion) while negative look-ahead assertion is (!?assertion).<\/p>\n<p>&nbsp;<\/p>\n<p>The look-ahead assertion (?=[[:digit:]]) is used to match the next character in the pattern must be a digit.<\/p>\n<p>&nbsp;<\/p>\n<p>The look-ahead assertion (?=.*[[:digit:]]) is used to match that the pattern must contain at-least one digit.<\/p>\n<p>&nbsp;<\/p>\n<p>Following code show another example of look-ahead assertion.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$pattern1 = &#8216;\/^(?=.*[[:digit:]])[[:alnum:]]{6}$\/&#8217;;<\/p>\n<p>$ans1 = preg_match($pattern1,&#8217;Paresh&#8217;);<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Ans1 : $ans1&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans2 = preg_match($pattern1,&#8217;Pares8&#8242;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Ans2 : $ans2&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/negative lookahead assertion<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$pattern2 = &#8216;\/^(?!3[2-9])[0-3][[:digit:]]$\/&#8217;;\u00a0 \/\/ values excluded between 32 to 39. Negative look-ahead<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans3 = preg_match($pattern2,&#8217;32&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Ans3 : $ans3&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$ans4 = preg_match($pattern2,&#8217;31&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Ans4 : $ans4&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/password complexity<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$pwd = &#8216;\/^(?=.*[[:digit:]])(?=.*[[:punct:]])[[:print:]]{6,}$\/&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 ^\u00a0 represent from the start of the pattern<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 (?=.*[[:digit:]]) represent that the assertion must contain atleast one digit<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 \u00a0(?=.*[[:punct:]]) represent that the assertion must containt atleast one punctuation character<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 \u00a0[[:print:]]{6,} represent that the pattern must have 6 or more printable characters<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0\u00a0\u00a0 $ represent from the end of the pattern<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$pass1 = &#8216;super3man&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$p1 = preg_match($pwd, $pass1);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; pass1 : $p1&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$pass2 = &#8216;supe2m@n&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$p2 = preg_match($pwd, $pass2);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; pass2 : $p2&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>The output :<\/p>\n<p>&nbsp;<\/p>\n<p>Ans1 : 0<\/p>\n<p>Ans2 : 1<\/p>\n<p>Ans3 : 0<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Ans4 : 1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">pass1 : 0<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">pass2 : 1<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Global Regular Expression<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A global regular expression is used to find multiple matches of a pattern in a string. To find multiple matches of a pattern in a string, preg_match_all function is used.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 151px; width: 734px;\">\n<tbody>\n<tr>\n<td style=\"width: 233.063px\"><strong>Function<\/strong><\/td>\n<td style=\"width: 472.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 233.063px\" rowspan=\"4\">preg_match_all($pattern,$string,<\/p>\n<p>$matches)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 472.063px\" rowspan=\"4\">Returns no. of matches found in a string. This function stores all the<\/p>\n<p>matched substrings in $matches. $matches is a multi-dimensional<\/p>\n<p>array. In $matches, $matches[0] (the first element) is an array of<\/p>\n<p>matched substring.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$string = &#8216;JTK-4702 JTK-1379&#8217;;<\/p>\n<p>$pattern = &#8216;\/JTK-[[:digit:]]{4}\/&#8217;;<\/p>\n<p>$count = preg_match_all($pattern,$string,$matches);<\/p>\n<p>foreach($matches[0] as $match)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; $match&#8221;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The output is:<\/p>\n<p>&nbsp;<\/p>\n<p>JTK-4702<\/p>\n<p>JTK-1379<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Replace a regular expression with string<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To replace the text which matches a pattern, preg_replace function is used. This function works similar to str_replace and stri_replace. However, preg_replace function allows to replace any text which can be matched with specified regular expression.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Function<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">preg_replace($pattern,$new,<\/p>\n<p>$string)<\/td>\n<td rowspan=\"2\">Returns a string which is created by\u00a0 replacing the $new of $string.<\/p>\n<p>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$items = &#8216;JTK-4702 JTM-3618&#8217;;<\/p>\n<p>$items = preg_replace(&#8216;\/JT[KM]\/&#8217;,&#8217;PCode&#8217;,$items);<\/p>\n<p>echo $items;<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The output is:<\/p>\n<p>&nbsp;<\/p>\n<p>PCode-4702 PCode-3618<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Following code shows few of data validation using regular expression<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/phone in 999-999-9999 format<\/p>\n<p>$phone = &#8216;\/^\\d{3}-\\d{3}-\\d{4}$\/&#8217;;<\/p>\n<p>$phone1 = &#8216;\/^[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$\/&#8217;; $p1 =&#8217;559-237-8024&#8242;;<\/p>\n<p>$ans1 = preg_match($phone,$p1);<\/p>\n<p>echo &#8220;&lt;br&gt; $ans1&#8221;;<\/p>\n<p>$ans11 = preg_match($phone1,$p1);<\/p>\n<p>echo &#8220;&lt;br&gt; Ans11 : $ans11&#8221;;<\/p>\n<p>\/\/\u00a0 Credit Card in 9999-9999-9999-9999-999 $creditcard = &#8216;\/^\\d{4}(-\\d{4}){3}$\/&#8217;; $card = &#8216;1234-5678-9012-3456&#8217;;<\/p>\n<p>$cardmatch = preg_match($creditcard,$card); echo &#8220;&lt;br&gt; Card Match is : $cardmatch&#8221;; \/\/Code in 99999 or 99999-9999<\/p>\n<p>$code = &#8216;\/^\\d{5}(-\\d{4}) ?$\/&#8217;; $zip1 = &#8216;12345&#8217;;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$zipm1 = preg_match($code,$zip1);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Zip1 $zip1 is matched&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$zip2 = &#8216;12345-6789&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$zipm2 = preg_match($code,$zip2);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Zip2 $zip2 is matched&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/date in format dd\/mm\/yyyy<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$date = &#8216;\/^(0?[1-9]|[12][[:digit:]]|[3][01])\\\/(0?[1-9]|1[012])\\\/[[:digit:]]{4}$\/&#8217;; $bday = &#8217;12\/6\/1985&#8242;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$bdmatch = preg_match($date, $bday);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Birthday : $bdmatch&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>The output will be:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1<\/p>\n<p>Ans11 : 1<\/p>\n<p>Card Match is : 1<\/p>\n<p>Zip1 12345 is matched<\/p>\n<p>Zip2 12345-6789 is matched<\/p>\n<p>Birthday : 1<\/p>\n<p>&nbsp;<\/p>\n<p><strong>References and suggested additional reading:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O&#8217;Reilly Media<\/p>\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP 5, Wrox<\/p>\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education<\/p>\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\n<p style=\"text-align: justify\">14. http:\/\/www.noupe.com\/development\/php-regular-expressions.html<\/p>\n<p style=\"text-align: justify\">15. http:\/\/php5.kiev.ua\/manual\/en\/regexp.reference.subpatterns.html<\/p>\n","protected":false},"author":3,"menu_order":16,"template":"","meta":{"_acf_changed":false,"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-179","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":5,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/179\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/179\/revisions\/181"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/179\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=179"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=179"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}