{"id":6122,"date":"2018-12-05T23:06:45","date_gmt":"2018-12-05T22:06:45","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=6122"},"modified":"2018-12-06T12:34:35","modified_gmt":"2018-12-06T11:34:35","slug":"powershell-escape-a-string-for-regular-expression","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/powershell-escape-a-string-for-regular-expression\/","title":{"rendered":"Powershell:  Escape a String for regular expression."},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_6122 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_6122')){$('.twoclick_social_bookmarks_post_6122').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Powershell%3A%20%20Escape%20a%20String%20for%20regular%20expression.\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Awhen%20a%20pattern%20for%20a%20regular%20expression%20contains%20reserved%20characters%20it%20can%20be%20difficult%20to%20quote%20all%20these%20signs%20correctly.%0D%0A%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/powershell-escape-a-string-for-regular-expression\\\/\",\"post_id\":6122,\"post_title_referrer_track\":\"Powershell%3A++Escape+a+String+for+regular+expression.\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>when a pattern for a regular expression contains reserved characters it can be difficult to quote all these signs correctly.<\/p>\n<p><!--more--><br \/>\nA .NET function helps \ud83d\ude42<\/p>\n<p>Let us assume you want to parse a HTML Site for download links. The lines with the link have the format<\/p>\n<p><code><br \/>\n&lt;li&gt;&lt;a href=\"http:\/\/cert.mydomain.org\/pki\/Cert0001.crt\" target=\"_blank\"&gt;download&lt;\/a&gt;&lt;\/li&gt;<br \/>\n<\/code><br \/>\nCharacters like &#8220;.&#8221; or Spaces must be escaped. The function Escape() from the RegEx class helps:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$sLink='&lt;li&gt;&lt;a href=&quot;http:\/\/cert.mydomain.org\/pki\/Cert0001.crt&quot; target=&quot;_blank&quot;&gt;download&lt;\/a&gt;&lt;\/li&gt;'\r\n$EscapedString=&#x5B;System.Text.RegularExpressions.Regex]::Escape($sLink)\r\n$EscapedString\r\n\r\n&lt;li&gt;&lt;a\\ href=&quot;http:\/\/cert\\.mydomain\\.org\/pki\/Cert0001\\.crt&quot;\\ target=&quot;_blank&quot;&gt;download&lt;\/a&gt;&lt;\/li&gt;\r\n\r\n<\/pre>\n<p>And then its simply to extract the download link(all crt files from cert.mydomain.org) :<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$sPattern='\r\n&lt;li&gt;&lt;a\\ href=&quot;(http:\/\/cert\\.mydomain\\.org\/pki\/.+\\.crt)&quot;\\ target=&quot;_blank&quot;&gt;download&lt;\/a&gt;&lt;\/li&gt;\r\n\r\n'\r\n&#x5B;System.Text.RegularExpressions.Regex]::Match($sLink, $sPattern).Groups&#x5B;1].Value\r\nhttp:\/\/cert.mydomain.org\/pki\/Cert0001.crt\r\n<\/pre>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, when a pattern for a regular expression contains reserved characters it can be difficult to quote all these signs correctly.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[344,1290],"tags":[1291,1295,133,1292,1294,1293],"class_list":["post-6122","post","type-post","status-publish","format-standard","hentry","category-powershell-scripting","category-regulare-expressions","tag-escape","tag-expression","tag-powershell","tag-quote","tag-regular","tag-string"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/6122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=6122"}],"version-history":[{"count":5,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/6122\/revisions"}],"predecessor-version":[{"id":6125,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/6122\/revisions\/6125"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=6122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=6122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=6122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}