{"id":729,"date":"2012-05-31T07:52:00","date_gmt":"2012-05-31T07:52:00","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=729"},"modified":"2012-06-20T13:27:28","modified_gmt":"2012-06-20T13:27:28","slug":"regular-expressions-part-2-matching-patterns","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/","title":{"rendered":"Regular Expressions  Part 2 &#8211; Matching Patterns"},"content":{"rendered":"<p><strong>Matching Patterns<\/strong><\/p>\n<p>The preg_match() function performs Perl-style pattern matching on a string. preg_match() takes two basic and three optional parameters. These parameters are, in order, a regular expression string, a source string, an array variable which stores matches, a flag argument and an offset parameter that can be used to specify the alternate place from which to start the search:<br \/>\npreg_match ( pattern, subject [, matches [, flags [, offset]]])<\/p>\n<p>The preg_match() function returns 1 if a match is found and 0 otherwise. Let&#8217;s search the string &#8220;Hello World!&#8221; for the letters &#8220;ll&#8221;:<\/p>\n<p>&lt;?php<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nif (preg_match(&#8220;\/ell\/&#8221;, &#8220;Hello World!&#8221;, $matches)) {<br \/>\necho &#8220;Match was found &lt;br \/&gt;&#8221;;<br \/>\necho $matches[0];<br \/>\n}<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n?&gt;<\/p>\n<p>The letters &#8220;ll&#8221; exist in &#8220;Hello&#8221;, so preg_match() returns 1 and the first element of the $matches variable is filled with the string that matched the pattern. The regular expression in the next example is looking for the letters &#8220;ell&#8221;, but looking for them with following characters:<\/p>\n<p>&lt;?php<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nif (preg_match(&#8220;\/ll.*\/&#8221;, &#8220;The History of Halloween&#8221;, $matches)) {<br \/>\necho &#8220;Match was found &lt;br \/&gt;&#8221;;<br \/>\necho $matches[0];<br \/>\n}<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n?&gt;<\/p>\n<p>Now let&#8217;s consider more complicated example. The most popular use of regular expressions is validation. The example below checks if the password is &#8220;strong&#8221;, i.e. the password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit:<\/p>\n<p>&lt;?php<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n$password = &#8220;Fyfjk34sdfjfsjq7&#8221;;<\/p>\n<p>if (preg_match(&#8220;\/^.*(?=.{8,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*$\/&#8221;, $password)) {<br \/>\necho &#8220;Your passwords is strong.&#8221;;<br \/>\n} else {<br \/>\necho &#8220;Your password is weak.&#8221;;<br \/>\n}<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n?&gt;<\/p>\n<p>The ^ and $ are looking for something at the start and the end of the string. The &#8220;.*&#8221; combination is used at both the start and the end. As mentioned above, the .(dot) metacharacter means any alphanumeric character, and * metacharacter means &#8220;zero or more&#8221;. Between are groupings in parentheses. The &#8220;?=&#8221; combination means &#8220;the next text must be like this&#8221;. This construct doesn&#8217;t capture the text. In this example, instead of specifying the order that things should appear, it&#8217;s saying that it must appear but we&#8217;re not worried about the order.<\/p>\n<p>The first grouping\u00a0 is (?=.*{8,}). This checks if there are at least 8 characters in the string. The next grouping (?=.*[0-9]) means &#8220;any alphanumeric character can happen zero or more times, then any digit can happen&#8221;. So this checks if there is at least one number in the string. But since the string isn&#8217;t captured, that one digit can appear anywhere in the string. The next groupings (?=.*[a-z]) and (?=.*[A-Z]) are looking for the lower case and upper case letter accordingly anywhere in the string.<\/p>\n<p>Finally, we will consider regular expression that validates an email address:<\/p>\n<p>&lt;?php<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n$email = firstname.lastname@aaa.bbb.com;<br \/>\n$regexp = &#8220;\/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$\/&#8221;;<\/p>\n<p>if (preg_match($regexp, $email)) {<br \/>\necho &#8220;Email address is valid.&#8221;;<br \/>\n} else {<br \/>\necho &#8220;Email address is &lt;u&gt;not&lt;\/u&gt; valid.&#8221;;<br \/>\n}<br \/>\n&#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n?&gt;<\/p>\n<p>This regular expression checks for the number at the beginning and also checks for multiple periods in the user name and domain name in the email address.<\/p>\n<p>For the speed reasons, the preg_match() function matches only the first pattern it finds in a string. This means it is very quick to check whether a pattern exists in a string. An alternative function, preg_match_all(), matches a pattern against a string as many times as the pattern allows, and returns the number of times it matched.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matching Patterns The preg_match() function performs Perl-style pattern matching on a string. preg_match() takes two basic and three optional parameters. These parameters are, in order, a regular expression string, a source string, an array variable which stores matches, a flag<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/\">Read more <span class=\"screen-reader-text\">Regular Expressions  Part 2 &#8211; Matching Patterns<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,16],"tags":[],"class_list":["post-729","post","type-post","status-publish","format-standard","hentry","category-general-php","category-reg-expressions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Regular Expressions Part 2 - Matching Patterns - PHP Web Applications<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Regular Expressions Part 2 - Matching Patterns - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"Matching Patterns The preg_match() function performs Perl-style pattern matching on a string. preg_match() takes two basic and three optional parameters. These parameters are, in order, a regular expression string, a source string, an array variable which stores matches, a flag&hellip;Read more Regular Expressions Part 2 &#8211; Matching Patterns &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-05-31T07:52:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-06-20T13:27:28+00:00\" \/>\n<meta name=\"author\" content=\"paulv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"paulv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-2-matching-patterns\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-2-matching-patterns\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"Regular Expressions Part 2 &#8211; Matching Patterns\",\"datePublished\":\"2012-05-31T07:52:00+00:00\",\"dateModified\":\"2012-06-20T13:27:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-2-matching-patterns\\\/\"},\"wordCount\":583,\"commentCount\":0,\"articleSection\":[\"General PHP\",\"Regular Expressions\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-2-matching-patterns\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-2-matching-patterns\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-2-matching-patterns\\\/\",\"name\":\"Regular Expressions Part 2 - Matching Patterns - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-05-31T07:52:00+00:00\",\"dateModified\":\"2012-06-20T13:27:28+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-2-matching-patterns\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-2-matching-patterns\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-2-matching-patterns\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Regular Expressions Part 2 &#8211; Matching Patterns\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\",\"name\":\"PHP Web Applications\",\"description\":\"Information and support for products of WithinWeb.com\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\",\"name\":\"paulv\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/author\\\/paulv\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Regular Expressions Part 2 - Matching Patterns - PHP Web Applications","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/","og_locale":"en_US","og_type":"article","og_title":"Regular Expressions Part 2 - Matching Patterns - PHP Web Applications","og_description":"Matching Patterns The preg_match() function performs Perl-style pattern matching on a string. preg_match() takes two basic and three optional parameters. These parameters are, in order, a regular expression string, a source string, an array variable which stores matches, a flag&hellip;Read more Regular Expressions Part 2 &#8211; Matching Patterns &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/","og_site_name":"PHP Web Applications","article_published_time":"2012-05-31T07:52:00+00:00","article_modified_time":"2012-06-20T13:27:28+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"Regular Expressions Part 2 &#8211; Matching Patterns","datePublished":"2012-05-31T07:52:00+00:00","dateModified":"2012-06-20T13:27:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/"},"wordCount":583,"commentCount":0,"articleSection":["General PHP","Regular Expressions"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/","url":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/","name":"Regular Expressions Part 2 - Matching Patterns - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-05-31T07:52:00+00:00","dateModified":"2012-06-20T13:27:28+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-2-matching-patterns\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"Regular Expressions Part 2 &#8211; Matching Patterns"}]},{"@type":"WebSite","@id":"https:\/\/www.withinweb.com\/info\/#website","url":"https:\/\/www.withinweb.com\/info\/","name":"PHP Web Applications","description":"Information and support for products of WithinWeb.com","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.withinweb.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c","name":"paulv","url":"https:\/\/www.withinweb.com\/info\/author\/paulv\/"}]}},"_links":{"self":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/comments?post=729"}],"version-history":[{"count":4,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":782,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/729\/revisions\/782"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}