{"id":783,"date":"2015-03-14T01:12:51","date_gmt":"2015-03-13T19:12:51","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=783"},"modified":"2016-04-02T00:26:51","modified_gmt":"2016-04-01T18:26:51","slug":"regular-expression-regex-tips-notepad","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/regular-expression-regex-tips-notepad\/","title":{"rendered":"Regular Expression (RegEx) Tips in Notepad++"},"content":{"rendered":"<p>Notepad++ is an awesome text editor and can do so much. \u00a0The find and replace tools allow for you to do regular expression finds and replaces (I believe you need the TextFX plugin installed). \u00a0This makes for a nearly limitless set of options! \u00a0But you need to know how to use it&#8230; \u00a0ha!<\/p>\n<p>These are a few things I tend to run into or get hung up on, so passing them along.<\/p>\n<figure id=\"attachment_784\" aria-describedby=\"caption-attachment-784\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/03\/RegEx-Patterns-For-HREF-Replacement.jpg\" rel=\"attachment wp-att-784\" data-lasso-id=\"343\" data-rel=\"lightbox-gallery-uwtDU53Q\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"size-medium wp-image-784\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/03\/RegEx-Patterns-For-HREF-Replacement-500x351.jpg\" alt=\"Regular Expressions for anchor tags\" width=\"500\" height=\"351\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/03\/RegEx-Patterns-For-HREF-Replacement-500x351.jpg 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/03\/RegEx-Patterns-For-HREF-Replacement-150x105.jpg 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/03\/RegEx-Patterns-For-HREF-Replacement-600x421.jpg 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/03\/RegEx-Patterns-For-HREF-Replacement.jpg 696w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-784\" class=\"wp-caption-text\">Use Regular Expressions in Notepad++ to match different patterns in anchor tags, allowing you to modify anchor tags in bulk.<\/figcaption><\/figure>\n<h2>Wildcard Match<\/h2>\n<p>Lets say you want to find all the anchor tags in your code &#8211; you can use a wildcard in place of where the href URL would be.<\/p>\n<pre title=\"Wildcard RegEx for Notepad++\">.*?<\/pre>\n<p>That wildcard expression will say &#8220;get me anything that is in this position.<\/p>\n<h3>Example<\/h3>\n<pre class=\"lang:html\" title=\"This Regular Expression\">&lt;a href=\".*?\"&gt;\r\n<\/pre>\n<pre class=\"lang:html\" title=\"Matches this\">&lt;a href=\"http:\/\/www.example.com\"&gt;\r\n<\/pre>\n<pre class=\"lang:html\" title=\"Does not match this - single quotes vs double quotes\">&lt;a href='http:\/\/www.example.com'&gt;\r\n<\/pre>\n<h2>Grouping<\/h2>\n<p>You can then group things together using parenthesis. \u00a0Each grouping then becomes another index in the output (used when doing a find\/replace) &#8211; I&#8217;ll explain more later on how that works.<\/p>\n<p>Continuing to use the href URL example above, we will improve our regular expression a bit to handle whether the &lt;a&gt; tag uses single or double quotes.<\/p>\n<pre title=\"Change this:\">\".*?\"\r\n<\/pre>\n<pre title=\"To this:\">(\"|').*?(\"|')\r\n<\/pre>\n<p>The double quotes in the regex become\u00a0(&#8220;|&#8217;). \u00a0The parenthesis create a group. \u00a0That group has two options &#8211; single quote or a double quote, which is separated by a pipe. \u00a0The pipe means or. \u00a0You can use more options by adding another pipe to separate the options.<\/p>\n<h3>Example<\/h3>\n<pre class=\"lang:default decode:true\" title=\"Regular Expression\">&lt;a href=(\"|'|&amp;).*?(\"|'|&amp;)&gt;<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Matches\">&lt;a href=\"http:\/\/www.example.com\"&gt;<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Matches\">&lt;a href='http:\/\/www.example.com'&gt;<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Matches\">&lt;a href=&amp;http:\/\/www.example.com&amp;&gt;<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Does Not Match\">&lt;a href=*http:\/\/www.example.com*&gt;<\/pre>\n<h2>Output<\/h2>\n<p>This is great that we can find stuff &#8211; but now lets make this more powerful. \u00a0Lets do a find and a replace &#8211; allowing us to bulk edit a document.<\/p>\n<p>I mentioned above that each time you group something with a parenthesis it will create an output group. \u00a0What this means is that you can then use that output group in the replace portion of you find\/replace tool in Notepad++.<\/p>\n<p>To illustrate this, let&#8217;s modify our last regular expression one more time &#8211; and what we are doing is adding a third grouping.<\/p>\n<pre><code>&lt;a href=(\"|'|&amp;)(.*?)(\"|'|&amp;)&gt;<\/code><\/pre>\n<p>So we created three groupings that can be used in the output. \u00a0Using the groupings is fairly straightforward &#8211; you use a backslash followed by the output number you&#8217;d like to use. \u00a0Output numbers start with an index of 1.<\/p>\n<h3>Example<\/h3>\n<pre class=\"lang:default decode:true\" title=\"Regular Expression\"># Find\r\n&lt;a href=(\"|'|&amp;)(.*?)(\"|'|&amp;)&gt;\r\n\r\n# Replace\r\n&lt;a href=\"\\2\" target=\"_blank\"&gt;<\/pre>\n<h4>Find\/Replace #1<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Matches\">&lt;a href=\"http:\/\/www.example.com\"&gt;\r\n# Output Group 1: \"\r\n# Output Group 2: http:\/\/www.example.com\r\n# Output Group 3: \"<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Replaced Output:\">&lt;a href=\"http:\/\/www.example.com\" target=\"_blank\"&gt;<\/pre>\n<h4>Find\/Replace #2<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Matches\">&lt;a href='http:\/\/www.example.com'&gt;\r\n# Output Group 1: '\r\n# Output Group 2: http:\/\/www.example.com\r\n# Output Group 3: '<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Replaced Output:\">&lt;a href='http:\/\/www.example.com' target=\"_blank\"&gt;<\/pre>\n<h4>Find\/Replace #3<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Matches\">&lt;a href=&amp;http:\/\/www.example.com&amp;&gt;\r\n# Output Group 1: &amp;\r\n# Output Group 2: http:\/\/www.example.com\r\n# Output Group 3: &amp;<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Replaced Output:\">&lt;a href=&amp;http:\/\/www.example.com&amp; target=\"_blank\"&gt;<\/pre>\n<h4>Find\/Replace #4<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Does Not Match\">&lt;a href=*http:\/\/www.example.com*&gt;<\/pre>\n<p>In this example, we are looking for one of three separators to wrap the URL, but our replace regular expression instructs it to only use double-quotes. \u00a0We then use the output from group #2 to keep the same URL. \u00a0Lastly, we add in a target=&#8221;_blank&#8221; into the &lt;a&gt; tag.<\/p>\n<p>If you wanted to keep the same URL wrapping separators you could easily do this as well:<\/p>\n<pre class=\"lang:default decode:true\" title=\"Regular Expression\"># Find\r\n&lt;a href=(\"|'|&amp;)(.*?)(\"|'|&amp;)&gt;\r\n\r\n# Replace\r\n&lt;a href=\\1\\2\\3 target=\"_blank\"&gt;<\/pre>\n<h4>Find\/Replace #1<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Matches\">&lt;a href=\"http:\/\/www.example.com\"&gt;\r\n# Matches - Output Group 1: \"\r\n# Output Group 2: http:\/\/www.example.com\r\n# Output Group 3: \"<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Replaced Output:\">&lt;a href=\"http:\/\/www.example.com\" target=\"_blank\"&gt;<\/pre>\n<h4>Find\/Replace #2<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Matches\">&lt;a href='http:\/\/www.example.com'&gt;\r\n# Output Group 1: '\r\n# Output Group 2: http:\/\/www.example.com\r\n# Output Group 3: '<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Replaced Output:\">&lt;a href=\"http:\/\/www.example.com\" target=\"_blank\"&gt;<\/pre>\n<h4>Find\/Replace #3<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Matches\">&lt;a href=&amp;http:\/\/www.example.com&amp;&gt;\r\n# Output Group 1: &amp;\r\n# Output Group 2: http:\/\/www.example.com\r\n# Output Group 3: &amp;<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Replaced Output:\">&lt;a href=\"http:\/\/www.example.com\" target=\"_blank\"&gt;<\/pre>\n<h4>Find\/Replace #4<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Does Not Match\">&lt;a href=*http:\/\/www.example.com*&gt;<\/pre>\n<p>&nbsp;<\/p>\n<h2>Remove the URL from an &lt;a&gt; HREF Tag<\/h2>\n<p>Ok, lets do one last thing. \u00a0Let&#8217;s say we just want to remove the HREF out of the &lt;a&gt; anchor tags. \u00a0Simple enough once again.<\/p>\n<h3>Example Using Same HREF Separators<\/h3>\n<pre class=\"lang:default decode:true\" title=\"Regular Expression\"># Find\r\n&lt;a href=(\"|'|&amp;).*?(\"|'|&amp;)&gt;\r\n\r\n# Replace\r\n&lt;a href=\\1\\2&gt;<\/pre>\n<h4>Find\/Replace #1<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Matched Content\">&lt;a href=\"http:\/\/www.example.com\"&gt;<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Replaced Output\">&lt;a href=\"#\"&gt;<\/pre>\n<h4>Find\/Replace #2<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Matched Content\">&lt;a href='http:\/\/www.example.com'&gt;<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Replaced Output\">&lt;a href=''&gt;<\/pre>\n<h4>Find\/Replace #3<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Matched Content\">&lt;a href=&amp;http:\/\/www.example.com&amp;&gt;<\/pre>\n<pre class=\"lang:default decode:true\" title=\"Replaced Output\">&lt;a href=&amp;&amp;&gt;<\/pre>\n<h4>Find\/Replace #4<\/h4>\n<pre class=\"lang:default decode:true\" title=\"Does Not Match\">&lt;a href=*http:\/\/www.example.com*&gt;<\/pre>\n<h3>Example Changing\u00a0the\u00a0HREF Separators<\/h3>\n<pre><code>Regular Expression (Find)\r\n&lt;a href=(\"|'|&amp;).*?(\"|'|&amp;)&gt;\r\n\r\nRegular Expression (Replace)\r\n&lt;a href=\"#\"&gt;\r\n\r\n\r\n\r\n&lt;a href=\"http:\/\/www.example.com\"&gt;\r\n - Matches\r\n   Replaced Output:\r\n   &lt;a href=\"#\"&gt;\r\n\r\n&lt;a href='http:\/\/www.example.com'&gt;\r\n - Matches\r\n   Replaced Output:\r\n   &lt;a href=\"#\"&gt;\r\n\r\n&lt;a href=&amp;http:\/\/www.example.com&amp;&gt;\r\n - Matches\r\n   Replaced Output:\r\n   &lt;a href=\"#\"&gt;\r\n\r\n&lt;a href=*http:\/\/www.example.com*&gt;\r\n - Does Not Match<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>I hope this helps to clear up some confusion!<\/p>\n<h2>Other RegEx Patterns<\/h2>\n<h3>Numbers<\/h3>\n<p>Let&#8217;s say you have a numbered list such as the following, and you want the text only.<\/p>\n<p>#1: &#8211; First Item<br \/> #2: &#8211; Second Item<br \/> #3: &#8211;\u00a0Third Item<\/p>\n<pre><code>Regular Expression (Find)\r\n#([0-9]*): -\u00a0\r\n\r\nRegular Expression (Replace)\r\n <em>(this space intentionally left blank)<\/em>\r\n\r\n\r\n<strong>Result:<\/strong>\r\nFirst Item\r\nSecond Item\r\nThird Item\r\n<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>Notepad++ is an awesome text editor and can do so much. \u00a0The find and replace tools allow for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":784,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[18,17,5],"tags":[],"class_list":["post-783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computers","category-development-software","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/03\/RegEx-Patterns-For-HREF-Replacement.jpg","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-cD","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":10,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":1340,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/783\/revisions\/1340"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/784"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}