{"id":719,"date":"2012-05-29T08:03:48","date_gmt":"2012-05-29T08:03:48","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=719"},"modified":"2012-06-20T13:27:09","modified_gmt":"2012-06-20T13:27:09","slug":"regular-expressions-part-1","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/","title":{"rendered":"Regular Expressions Part 1 &#8211; Introduction"},"content":{"rendered":"<p>In programming you quite often want to test a string for the occurrence of a character or group of characters.\u00a0 This is particularly true for such things as data validation on form input boxes.\u00a0 Regular expressions provide a pattern matching facility.<\/p>\n<p>This tutorial gives a brief overview of basic regular expression syntax and then considers the functions that PHP provides for<br \/>\nworking with regular expressions.<\/p>\n<p>The Basics<br \/>\nMatching Patterns<br \/>\nReplacing Patterns<br \/>\nArray Processing<\/p>\n<p>PHP supports two different types of regular expressions: POSIX-extended and Perl-Compatible Regular Expressions (PCRE). The PCRE functions are more powerful than the POSIX ones, and faster too, so we will concentrate on them.<\/p>\n<p><strong>The Basics <\/strong><\/p>\n<p>In a regular expression, most characters match only themselves. For instance, if you search for the regular expression &#8220;ball&#8221; in the string &#8220;John plays football,&#8221; you get a match because &#8220;ball&#8221; occurs in that string. Some characters have special meanings in regular expressions. For instance, a dollar sign ($) is used to match strings that end with the given pattern. Similarly, a caret (^)<br \/>\ncharacter at the beginning of a regular expression indicates that it must match the beginning of the string. The characters that<br \/>\nmatch themselves are called literals. The characters that have special meanings are called metacharacters.<\/p>\n<p>The dot (.) metacharacter matches any single character except newline (\\). So, the pattern h.t matches hat, hothit, hut, h7t, etc. The vertical pipe (|) metacharacter is used for alternatives in a regular expression. It behaves much like a logical OR operator and you should use it if you want to construct a pattern that matches more than one set of characters. For instance, the pattern Utah| Idaho|Nevada matches strings that contain &#8220;Utah&#8221; or &#8220;Idaho&#8221; or &#8220;Nevada&#8221;. Parentheses give us a way to group sequences.<\/p>\n<p>For example :<\/p>\n<p>(Nant|b)ucket matches &#8220;Nantucket&#8221; or &#8220;bucket&#8221;. Using parentheses to group together characters for alternation is called grouping.<\/p>\n<p>If you want to match a literal metacharacter in a pattern, you have to escape it with a backslash.<\/p>\n<p>To specify a set of acceptable characters in your pattern, you can either build a character class yourself or use a predefined one.<\/p>\n<p>A character class lets you represent a bunch of characters as a single item in a regular expression. You can build your own character class by enclosing the acceptable characters in square brackets. A character class matches any one of the characters in<br \/>\nthe class. For example a character class [abc] matches a, b or c. To define a range of characters, just put the first and last<br \/>\ncharacters in, separated by hyphen. For example, to match all alphanumeric characters: [a-zA-Z0-9]. You can also create a negated character class, which matches any character that is not in the class. To create a negated character class, begin the character class with ^: [^0-9].<\/p>\n<p>The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched. + means &#8220;Match one or more of the<br \/>\npreceding expression&#8221;, * means &#8220;Match zero or more of the preceding expression&#8221;, and ? means &#8220;Match zero or one of the preceding expression&#8221;. Curly braces {} can be used differently. With a single integer, {n} means &#8220;match exactly n occurrences of the preceding expression&#8221;, with one integer and a comma, {n,} means &#8220;match n or more occurrences of the preceding expression&#8221;, and with two comma-separated integers {n,m} means &#8220;match the previous character if it occurs at least n times, but no more than m times&#8221;.<\/p>\n<p>Now, have a look at the examples:<\/p>\n<p>Regular Expression \u00a0\u00a0 \u00a0Will match&#8230;<br \/>\nfoo \u00a0\u00a0 \u00a0The string &#8220;foo&#8221;<br \/>\n^foo \u00a0\u00a0 \u00a0&#8220;foo&#8221; at the start of a string<br \/>\nfoo$ \u00a0\u00a0 \u00a0&#8220;foo&#8221; at the end of a string<br \/>\n^foo$ \u00a0\u00a0 \u00a0&#8220;foo&#8221; when it is alone on a string<br \/>\n[abc] \u00a0\u00a0 \u00a0a, b, or c<br \/>\n[a-z] \u00a0\u00a0 \u00a0Any lowercase letter<br \/>\n[^A-Z] \u00a0\u00a0 \u00a0Any character that is not a uppercase letter<br \/>\n(gif|jpg) \u00a0\u00a0 \u00a0Matches either &#8220;gif&#8221; or &#8220;jpeg&#8221;<br \/>\n[a-z]+ \u00a0\u00a0 \u00a0One or more lowercase letters<br \/>\n[0-9\\.\\-] \u00a0\u00a0 \u00a0?ny number, dot, or minus sign<br \/>\n^[a-zA-Z0-9_]{1,}$ \u00a0\u00a0 \u00a0Any word of at least one letter, number or _<br \/>\n([wx])([yz]) \u00a0\u00a0 \u00a0wy, wz, xy, or xz<br \/>\n[^A-Za-z0-9] \u00a0\u00a0 \u00a0Any symbol (not a number or a letter)<br \/>\n([A-Z]{3}|[0-9]{4}) \u00a0\u00a0 \u00a0Matches three letters or four numbers<\/p>\n<p>Perl-Compatible Regular Expressions emulate the Perl syntax for patterns, which means that each pattern must be enclosed in a pair of delimiters. Usually, the slash (\/) character is used. For instance, \/pattern\/.<\/p>\n<p>The PCRE functions can be divided in several classes: matching, replacing, splitting and filtering.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In programming you quite often want to test a string for the occurrence of a character or group of characters.\u00a0 This is particularly true for such things as data validation on form input boxes.\u00a0 Regular expressions provide a pattern matching<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/\">Read more <span class=\"screen-reader-text\">Regular Expressions Part 1 &#8211; Introduction<\/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-719","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.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Regular Expressions Part 1 - Introduction - 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-1\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Regular Expressions Part 1 - Introduction - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"In programming you quite often want to test a string for the occurrence of a character or group of characters.\u00a0 This is particularly true for such things as data validation on form input boxes.\u00a0 Regular expressions provide a pattern matching&hellip;Read more Regular Expressions Part 1 &#8211; Introduction &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-05-29T08:03:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-06-20T13:27:09+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=\"4 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-1\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-1\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"Regular Expressions Part 1 &#8211; Introduction\",\"datePublished\":\"2012-05-29T08:03:48+00:00\",\"dateModified\":\"2012-06-20T13:27:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-1\\\/\"},\"wordCount\":732,\"commentCount\":0,\"articleSection\":[\"General PHP\",\"Regular Expressions\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-1\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-1\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-1\\\/\",\"name\":\"Regular Expressions Part 1 - Introduction - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-05-29T08:03:48+00:00\",\"dateModified\":\"2012-06-20T13:27:09+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-1\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-1\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/regular-expressions-part-1\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Regular Expressions Part 1 &#8211; Introduction\"}]},{\"@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 1 - Introduction - 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-1\/","og_locale":"en_US","og_type":"article","og_title":"Regular Expressions Part 1 - Introduction - PHP Web Applications","og_description":"In programming you quite often want to test a string for the occurrence of a character or group of characters.\u00a0 This is particularly true for such things as data validation on form input boxes.\u00a0 Regular expressions provide a pattern matching&hellip;Read more Regular Expressions Part 1 &#8211; Introduction &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/","og_site_name":"PHP Web Applications","article_published_time":"2012-05-29T08:03:48+00:00","article_modified_time":"2012-06-20T13:27:09+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"Regular Expressions Part 1 &#8211; Introduction","datePublished":"2012-05-29T08:03:48+00:00","dateModified":"2012-06-20T13:27:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/"},"wordCount":732,"commentCount":0,"articleSection":["General PHP","Regular Expressions"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/","url":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/","name":"Regular Expressions Part 1 - Introduction - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-05-29T08:03:48+00:00","dateModified":"2012-06-20T13:27:09+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/regular-expressions-part-1\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"Regular Expressions Part 1 &#8211; Introduction"}]},{"@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\/719","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=719"}],"version-history":[{"count":5,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/719\/revisions"}],"predecessor-version":[{"id":781,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/719\/revisions\/781"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}