{"id":1288,"date":"2018-01-19T19:22:39","date_gmt":"2018-01-19T19:22:39","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/advanced-string-processing-how-regular-are-your-expressions\/"},"modified":"2018-01-19T19:24:40","modified_gmt":"2018-01-19T19:24:40","slug":"advanced-string-processing-how-regular-are-your-expressions","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/advanced-string-processing-how-regular-are-your-expressions\/","title":{"rendered":"Advanced String Processing &#8211; How Regular Are Your Expressions"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By PHP Builder Staff<\/div>\n<div class=\"\">on May 19, 2009<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<div class=\"articleHeader\">\nIntroduction\n<\/div>\n<div class=\"articlePara\"> Hello hello hello, and welcome<br \/>\nback. We&#8217;ve looked at strings, and numbers and all sorts of<br \/>\ntypes of data, but we&#8217;ve not yet seen how to do something<br \/>\nreally important, and that&#8217;s to look for and pull<br \/>\ninteresting parts out of the data we have, to do that where<br \/>\ngoing to use some magic from the Perl world called &#8220;Regular<br \/>\nExpressions&#8221; <\/div>\n<div class=\"articleHeader\">\nHuh, Regular What?\n<\/div>\n<div class=\"articlePara\"> Put simply, a regular expression<br \/>\nis a string in it&#8217;s own right, but one that has a special<br \/>\nmeaning.  In some ways it&#8217;s like a little mini program that<br \/>\ntells the regexp engine what to look for and how to find it.\n<\/div>\n<div class=\"articlePara\"> Look at the first line of my<br \/>\nopening paragraph above. <\/div>\n<div class=\"articlePara\"> If you wanted to look for that<br \/>\n&#8220;Hello hello hello&#8221; and treat it as a spelling error to<br \/>\ncorrect how are you going to find it? <\/div>\n<div class=\"articlePara\">\nWell you could use\n<\/div>\n<p>\n<code><br \/>\nif($text == \"Hello hello hello\")<br \/>\n<\/code>\n<\/p>\n<div class=\"articlePara\">\nor you might use the text function &#8220;<code>str_replace<\/code>&#8220;:\n<\/div>\n<p>\n<code><br \/>\nstr_replace(\"Hello hello hello\",\"Hello\");<br \/>\n<\/code>\n<\/p>\n<div class=\"articlePara\"> and they would work fine, but what<br \/>\nif we now made the phrase &#8220;Hello hello hullo&#8221;?  hmmm, it<br \/>\nlooks like we now don&#8217;t have a match.  This is where the<br \/>\npower of regular expressions comes to the rescue. <\/div>\n<div class=\"articleHeader\">\nHow do Regular Expressions Work Then?\n<\/div>\n<div class=\"articlePara\"> Ok, so your asking yourself, how<br \/>\ncan I match something that&#8217;s not match-able unless I change<br \/>\nwhat I&#8217;m looking for, which is not what I want to do. <\/div>\n<div class=\"articlePara\"> The key is not to change what your<br \/>\nsearching for but, to just search for the differences.\n<\/div>\n<div class=\"articlePara\"> It&#8217;s a set of searching rules,<br \/>\nthat allows for variations in the text to be searched.\n<\/div>\n<div class=\"articlePara\"> What exactly is the difference<br \/>\nbetween &#8220;Hello hello hello&#8221; and &#8220;Hello hello hullo&#8221; , well<br \/>\nin this case it&#8217;s only one letter, and that letter can be<br \/>\neither an &#8216;e&#8217; or a &#8216;u&#8217;, if we had a way of just saying<br \/>\nsearch for this phrase, but at the 4th letter from the end,<br \/>\nyou need to be aware that that could change, then you&#8217;ve<br \/>\npretty much defined what a regular expression is. <\/div>\n<div class=\"articlePara\"> Now in our example here, we could<br \/>\nactually expand that very easily, to be aware of any letter<br \/>\nat that position and not just a &#8216;e&#8217; or &#8216;u&#8217;, we do this by<br \/>\nusing the full stop operator &#8216;<code>.<\/code>&#8216; , so to show<br \/>\nyou what I mean we could write our search pattern like this:\n<\/div>\n<p>\n<code><br \/>\n\"Hello hello h.llo\"<br \/>\n<\/code>\n<\/p>\n<\/p>\n<div class=\"articlePara\"> That will match any character at<br \/>\nthat position, but only one character, all the rest have to<br \/>\nmatch. <\/div>\n<div class=\"articlePara\"> There is however much more to the<br \/>\npower of reg-expressions than just single letters, we can<br \/>\nsearch for whole groups of numbers, letters and combinations<br \/>\nof words symbols. Also for certain counts and lengths.\n<\/div>\n<div class=\"articlePara\"> There&#8217;s far more than we can cover<br \/>\nin this article, developing a true mastery of reg-<br \/>\nexpressions takes years. We only have time to cover what you<br \/>\nneed to know to get you started in PHP. <\/div>\n<div class=\"articlePara\"> Once snippet of advice I will<br \/>\ngive, find a program that will allow you to see what your<br \/>\ndoing as you construct regular expressions. I use the<br \/>\nwonderful reg-ex coach available from <a href=\"http:\/\/weitz.de\/regex-coach\/\">http:\/\/weitz.de\/regex-<br \/>\ncoach\/<\/a> There is an older version for linux, but the<br \/>\nlatest versions are only maintained under Windows now, it<br \/>\ndoes however run perfectly fine under wine. <\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"peter_shaw051820094658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"peter_shaw051820099ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"peter_shaw051820094658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This week Peter Shaw shows us the power of regular expressions, the building blocks of PHP, and what they can do for<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1288","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1288"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1288\/revisions"}],"predecessor-version":[{"id":3181,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1288\/revisions\/3181"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}