{"id":3945,"date":"2022-01-20T18:24:13","date_gmt":"2022-01-20T18:24:13","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3945"},"modified":"2022-02-17T07:13:46","modified_gmt":"2022-02-17T07:13:46","slug":"regex","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/regex-regular-expressions\/regex","title":{"rendered":".Regex"},"content":{"rendered":"\n<p>Regular expressions operate by moving character by character, from left to right, through a piece of text. When regex finds a character that matches the first piece of the expression, it looks to find a continuous sequence of matching characters.<\/p>\n\n\n<p><strong>Match text<\/strong><\/p>\n<pre>abc<\/pre>\n<p style=\"padding-left: 40px;\">Match the string &#8220;abc&#8221;<\/p>\n<p><strong>Alternation<\/strong><\/p>\n<pre>abc|def<\/pre>\n<p style=\"padding-left: 40px;\">Match the string &#8220;abc&#8221; OR &#8220;def&#8221;<br>Note that the | symbol matches the entire expression before or after itself.&nbsp; Use Grouping (see below) to OR specific parts of the regex expression.&nbsp; e.g. &#8220;Hello (Dave|David)!&#8221;<\/p>\n<p><strong>Character Sets<\/strong><\/p>\n<p>Square brackets [] are used to match one character from a series of possible characters<\/p>\n<pre>ab[cd]ef[gh ]ij<\/pre>\n<p style=\"padding-left: 40px;\">Match the string where any combination of the series characters are used, e.g.: &#8220;abcefgij&#8221; or &#8220;abdefgij&#8221; or &#8220;abdefhij&#8221; or &#8220;abdef ij&#8221;<br>&#8220;abcdefgij&#8221;, for example, will not match.<\/p>\n<p>^ character negates the character set, so will allow any character OTHER than the characters used<\/p>\n<pre>ab[^cd]ef[gh]ij<\/pre>\n<p style=\"padding-left: 40px;\">&#8220;abzefgij&#8221; would match, &#8220;abdefgij&#8221; would not.<\/p>\n<p><strong>Wildcards<\/strong><\/p>\n<p>. =&nbsp;Match a single character<\/p>\n<pre>...<\/pre>\n<p style=\"padding-left: 40px;\">Will match any 3 characters, but there must be 3 characters.<br>(If you need o use a period as a character in a regex expression, you escape it with a forward slash &#8220;\\.&#8221; )<\/p>\n<pre>...\\.<\/pre>\n<p style=\"padding-left: 40px;\">&#8220;qur.&#8221; would match, &#8220;qurk&#8221; wouldn&#8217;t match<\/p>\n<p><em>See also the &#8220;Quantifiers &#8211; 0 or More, 1 or More&#8221; section below for multicharacter wildcard options<\/em><\/p>\n<p><strong>Ranges<\/strong><\/p>\n<p>Use a hyphen to specify a range of character values that are allowed (remember we are still only matching to a single character)<\/p>\n<p>[a-c] is the same as saying [abc]<\/p>\n<p>[a-cA-D] is the same as saying [abcABCD]<\/p>\n<pre>[a-z]<br>[A-Z]<br>[a-zA-Z]<br>[0-9]<br>[a-zA-Z0-9]<\/pre>\n<p style=\"padding-left: 40px;\">Some typical ranges that are often used<\/p>\n<p><strong>Shorthand Character Classes<\/strong><\/p>\n<pre>\\w<\/pre>\n<p style=\"padding-left: 40px;\">\u201cword character\u201d class, represents regex range [A-Za-z0-9_]<\/p>\n<pre>\\d<\/pre>\n<p style=\"padding-left: 40px;\">\u201cdigit character\u201d class, represents regex range [0-9]<\/p>\n<pre>\\s<\/pre>\n<p style=\"padding-left: 40px;\">\u201cwhitespace character\u201d, represents regex range [ \\t\\r\\n\\f\\v]<br>(Matches a single space, tab, carriage return, line break, form feed, or vertical tab)<\/p>\n<pre>\\W<\/pre>\n<p style=\"padding-left: 40px;\">\u201cnon-word character\u201d class,&nbsp; represents regex range [^A-Za-z0-9_] (any character not included in the range represented by \\w )<\/p>\n<pre>\\D<\/pre>\n<p style=\"padding-left: 40px;\">\u201cnon-digit character\u201d class, represents regex range [^0-9] (any character not included in the range represented by \\d )<\/p>\n<pre>\\S<\/pre>\n<p style=\"padding-left: 40px;\">\u201cnon-whitespace character\u201d, represents regex range [^ \\t\\r\\n\\f\\v] (any character that is not included in the range represented by \\s )<\/p>\n<p>An example: \\d\\s\\w\\w<\/p>\n<p style=\"padding-left: 40px;\">Matches a digit character, followed by a whitespace character, followed by 2 word characters<\/p>\n<p><strong>Grouping<\/strong><\/p>\n<pre>()<\/pre>\n<p style=\"padding-left: 40px;\">Groups a section of the regex expression.&nbsp; E.g. Hello (Dave|David)!<br>will match for &#8220;Hello Dave!&#8221;, &#8220;Hello David!&#8221;,&nbsp;<\/p>\n<p><strong>Quantifiers &#8211; Fixed<\/strong><\/p>\n<p>Specify the number of characters to match using the last expression<\/p>\n<pre>{}<\/pre>\n<p style=\"padding-left: 40px;\">Specify the quantity of the previous character definition to match<br>&#8220;\\w{4}&#8221; would match exactly 4 word characters<br>&#8220;\\w{2,5}&#8221; would match min 2 word characters and max 5 word characters.&nbsp; The quantifier is greedy &#8211; it will match the greatest quantity of characters it possibly can when a min and max is specified.&nbsp;<\/p>\n<p><strong>Quantifiers &#8211; Optional<\/strong><\/p>\n<p>Specify that the previopus character definition is optional<\/p>\n<pre>?<\/pre>\n<p>Some examples:<\/p>\n<p style=\"padding-left: 40px;\">&#8220;abc?d&#8221; will match for &#8220;abd&#8221; and &#8220;abcd&#8221;<br>&#8220;Hello (cheeky )?boy&#8221;, will match for &#8220;Hello boy&#8221; and &#8220;Hello cheeky boy&#8221;<br>&#8220;Hello (loud |cheeky )?boy&#8221;, will match for &#8220;Hello boy&#8221;,&nbsp; &#8220;Hello cheeky boy&#8221; and &#8220;Hello loud boy&#8221;<\/p>\n<p><strong>Quantifiers &#8211; 0 or More, 1 or More<\/strong><\/p>\n<pre>*<\/pre>\n<p style=\"padding-left: 40px;\">Preceeding character doesn&#8217;t need to appear, or can appear an unlimited number of times<\/p>\n<pre>+<\/pre>\n<p style=\"padding-left: 40px;\">Preceeding character must appear 1 or more times<\/p>\n<p>Examples<\/p>\n<p style=\"padding-left: 40px;\">&#8220;abc*d&#8221; will match for &#8220;abd&#8221;, &#8220;abcd&#8221;, &#8220;abcccccd&#8221;, etc<br>&#8220;abc+d&#8221; will match for &#8220;abcd&#8221;, &#8220;abcccccd&#8221;, etc, but not for &#8220;abd&#8221;<\/p>\n<p><\/p>\n<p><strong>Anchors (start and end markers)<\/strong><\/p>\n<pre>^<\/pre>\n<p style=\"padding-left: 40px;\">Start of string marker &#8211; will not match if tested string starts before this point<\/p>\n<pre>$<\/pre>\n<p style=\"padding-left: 40px;\">End of strng marker &#8211; will not match if tested string continues past this point<\/p>\n<p>Examples<\/p>\n<p style=\"padding-left: 40px;\">&#8220;^bcd ef&#8221; will match &#8220;bcd ef&#8221;, &#8220;bcd efghi&#8221;, etc, but not match for &#8220;abcd ef&#8221;<br>&#8220;^bcd ef$&#8221; will match &#8220;bcd ef&#8221;, but not match for &#8220;abcd ef&#8221; or &#8220;bcd efghi&#8221;<\/p>\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\">Special character escaping<\/h4>\n\n\n\n<p>Characters used as special characters in regex that you need to escape if you want to specify that actual character in your regex:<\/p>\n\n\n<pre>\\\\<br>\\.<br>\\?<br>\\*<br>\\+<br>\\{<br>\\(<br>\\)<br>\\^<br>\\$<br>\\[<br>\\|<\/pre>\n<p><\/p>\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Regular expressions operate by moving character by character, from left to right, through a piece of text. When regex finds a character that matches the first piece of the expression, it looks to find a continuous sequence of matching characters. Match text abc Match the string &#8220;abc&#8221; Alternation abc|def Match the string &#8220;abc&#8221; OR &#8220;def&#8221;Note [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-3945","post","type-post","status-publish","format-standard","hentry","category-regex-regular-expressions"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3945"}],"version-history":[{"count":29,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3945\/revisions"}],"predecessor-version":[{"id":3997,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3945\/revisions\/3997"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}