{"id":830,"date":"2014-09-29T19:37:29","date_gmt":"2014-09-29T19:37:29","guid":{"rendered":"https:\/\/alt2.minisoft.com\/support\/?p=830"},"modified":"2020-06-10T12:23:07","modified_gmt":"2020-06-10T19:23:07","slug":"regular-expressions-lookaround","status":"publish","type":"post","link":"https:\/\/c002.minisoft.com\/support\/regular-expressions-lookaround\/","title":{"rendered":"Regular Expressions: LookAround"},"content":{"rendered":"<p>Regular expression lookaround assertions look at the content around the main portion of the regular expression for either a positive or negative match. These can be useful when the desired content is only distinct\u00a0from other content on your page by what is consistently surrounding it.<\/p>\n<table style=\"height: 184px;\" width=\"352\">\n<tbody>\n<tr>\n<td width=\"151\"><strong>Negative Lookahead<\/strong><\/td>\n<td width=\"48\">?!<\/td>\n<\/tr>\n<tr>\n<td width=\"151\"><strong>Positive\u00a0 Lookahead<\/strong><\/td>\n<td width=\"48\">?=<\/td>\n<\/tr>\n<tr>\n<td width=\"151\"><strong>Negative Lookbehind<\/strong><\/td>\n<td width=\"48\">?&lt;!<\/td>\n<\/tr>\n<tr>\n<td width=\"151\"><strong>Positive Lookbehind<\/strong><\/td>\n<td width=\"48\">?&lt;=<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Example:<\/h3>\n<p>Observe the list of order numbers below. Notice that some have white space before them, some have letters at the end of the number and one has a typo.<\/p>\n<div class=\"snippetBox\">\n<pre>  Order No: 1138a<\/pre>\n<pre>Order No: 1138b<\/pre>\n<pre>  aOrder No: 1973<\/pre>\n<pre>Order No: 1973b<\/pre>\n<\/div>\n<div class=\"color-bar\">\n<h3>Getting Started<\/h3>\n<\/div>\n<p>For this example, we will use variations of the same regex pattern: <strong>Order\\sNo:\\s\\d{4}<\/strong>.<br \/>\n<strong>Order\\sNo<\/strong>:\u00a0matches <em>Order No:<\/em>\u00a0by explicitly stating the letters to match on with the included space character <strong>\\s<\/strong>\u00a0between the words.<br \/>\nThe remainder of the pattern, <strong>\\s\\d{4}\u00a0<\/strong>dictates that a space and four digits must be present for a match.<\/p>\n<p>For more on Regular Expressions, go to the main <a href=\"\/support\/index.php\/regular-expressions\/\">Regex page<\/a>.<\/p>\n<div class=\"color-bar\">\n<h3>Lookahead<\/h3>\n<\/div>\n<p><strong>Positive Lookahead<\/strong><br \/>\nTo match on all the order numbers followed by a letter, we will use a Positive Lookahead <em>(?=)<\/em>.<\/p>\n<p>The regular expression <strong><em>Order\\sNo:\\s\\d{4}(?=[a-z])<\/em><\/strong> matches the highlighted lines.<\/p>\n<p><strong>*Note:<\/strong> [a-z] matches on any character of the alphabet a-z. Brackets may contain any grouping of characters to match on. In this instance, [ab] would have returned the same result.<\/p>\n<div class=\"snippetBox\">\n<pre><span style=\"background: #ffff00;\"> Order No: 1138<\/span>a<\/pre>\n<pre><span style=\"background: #ffff00;\">Order No: 1138<\/span>b<\/pre>\n<pre>  aOrder No: 1973<\/pre>\n<pre><span style=\"background: #ffff00;\">Order No: 1973<\/span>b<\/pre>\n<\/div>\n<p><strong>Negative Lookahead<\/strong><br \/>\nInversely, to match on only the order numbers without letters following the digits, a negative lookahead can be used <em>(?!)<\/em>.<\/p>\n<p>The pattern <em><strong>Order\\sNo:\\s\\d{4}(?![a-z])<\/strong><\/em> matches the highlighted lines.<\/p>\n<div class=\"snippetBox\">\n<pre>  Order No: 1138a<\/pre>\n<pre>Order No: 1138b<\/pre>\n<pre>  a<span style=\"background: #ffff00;\">Order No: 1973<\/span><\/pre>\n<pre>Order No: 1973b\n\n<\/pre>\n<\/div>\n<div class=\"color-bar\">\n<h3>Lookbehind<\/h3>\n<\/div>\n<p>Lookbehinds work in a similar manner to lookahead assertions. Rather than the\u00a0pattern being placed after the match string as they are with lookaheads, the lookbehind pattern goes prior to the string.<\/p>\n<p><strong>Positive Lookbehind<\/strong><\/p>\n<p>To look behind the Order No: for an instance of a another letter, a typo, a positive lookbehind can be used<em>(?&lt;=)<\/em>.<\/p>\n<p>The pattern<strong> <em>(?&lt;=a)Order\\sNo:\\s\\d{4}<\/em><\/strong>\u00a0matches the highlighted lines.<\/p>\n<div class=\"snippetBox\">\n<pre>  Order No: 1138a<\/pre>\n<pre>Order No: 1138b<\/pre>\n<pre>  a<span style=\"background: #ffff00;\">Order No: 1973<\/span><\/pre>\n<pre>Order No: 1973b<\/pre>\n<\/div>\n<p><strong>Negative Lookbehind<\/strong><\/p>\n<p>The inverse can be matched by using a negative lookbehind instead of a positive lookbehind.<\/p>\n<p>The pattern <strong><em>(?&lt;!a)Order\\sNo:\\s\\d{4}<\/em> <\/strong>matches the highlighted lines.<\/p>\n<div class=\"snippetBox\">\n<pre><span style=\"background: #ffff00;\">Order No: 1138a<\/span><\/pre>\n<pre><span style=\"background: #ffff00;\">Order No: 1138b<\/span><\/pre>\n<pre>  aOrder No: 1973<\/pre>\n<pre><span style=\"background: #ffff00;\">Order No: 1973b<\/span><\/pre>\n<\/div>\n<div><img decoding=\"async\" style=\"display: inline; width: 25px;\" src=\"https:\/\/alt2.minisoft.com\/support\/wp-content\/uploads\/2014\/08\/page.png\" alt=\"\" \/><strong style=\"font-size: 18px; padding-left: 15px; margin-top: 25px;\">Additional Resources<\/strong><\/div>\n<p><a title=\"regular-expressions.info\" href=\"http:\/\/www.regular-expressions.info\/lookaround.html\" target=\"_blank\" rel=\"noopener noreferrer\">More On LookAround Matching (Off-site)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Regular expression lookaround assertions look at the content around the main portion of the regular expression for either a positive or negative match. These can be useful when the desired content is only distinct\u00a0from other content on your page by what is consistently surrounding it. Negative Lookahead ?! Positive\u00a0 Lookahead ?= Negative Lookbehind ?&lt;! Positive [&hellip;]<\/p>\n","protected":false},"author":75,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,58],"tags":[5,6],"_links":{"self":[{"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/posts\/830"}],"collection":[{"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/users\/75"}],"replies":[{"embeddable":true,"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/comments?post=830"}],"version-history":[{"count":60,"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/posts\/830\/revisions"}],"predecessor-version":[{"id":7988,"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/posts\/830\/revisions\/7988"}],"wp:attachment":[{"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/media?parent=830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/categories?post=830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c002.minisoft.com\/support\/wp-json\/wp\/v2\/tags?post=830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}