{"id":652,"date":"2011-07-28T11:12:13","date_gmt":"2011-07-28T10:12:13","guid":{"rendered":"http:\/\/trigonakis.com\/blog\/?p=652"},"modified":"2011-07-28T11:14:16","modified_gmt":"2011-07-28T10:14:16","slug":"pattern-matching-hyphen-minus-sign-in-bash","status":"publish","type":"post","link":"http:\/\/trigonakis.com\/blog\/2011\/07\/28\/pattern-matching-hyphen-minus-sign-in-bash\/","title":{"rendered":"Pattern Matching Hyphen-Minus Sign in Bash"},"content":{"rendered":"<p>I was trying to use the <code>sed<\/code> command to perform some changes to a text and stepped into an interesting &#8220;problem&#8221;; <strong> pattern matching the minus-hyphen (-) symbol<\/strong>.<\/p>\n<p>Assume we have the following text:<\/p>\n<pre>\r\nsomething\r\nSoMeThiNg\r\nsome-thing\r\nsoMe_thing\r\n<\/pre>\n<p>and we want to match all the different versions of the word with one expression (one by one).<\/p>\n<p>My initial idea was to use this regular expression:<\/p>\n<pre lang=\"bash\">\r\n's\/[a-zA-Z\\-\\_]*\/matched\/'\r\n<\/pre>\n<p>Naturally, I tried to escape the &#8211; sign. As you can see from the output, this doesn&#8217;t work:<\/p>\n<pre lang=\"bash\">\r\n$ sed 's\/[a-zA-Z\\-\\_]*\/matched\/' test \r\nmatched\r\nmatched\r\nmatched-thing\r\nmatched\r\n<\/pre>\n<p>The minus sign is not matched, because of its special meaning (setting ranges). In order to make the expression work, you need to move the &#8220;-&#8221; either in the beginning or in the end of the expression:<\/p>\n<pre lang=\"bash\">\r\n$ sed 's\/[a-zA-Z\\_-]*\/matched\/' test \r\nmatched\r\nmatched\r\nmatched\r\nmatched\r\n$ sed 's\/[-a-zA-Z\\_]*\/matched\/' test \r\nmatched\r\nmatched\r\nmatched\r\nmatched\r\n<\/pre>\n<p>and leave it un-escaped!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to use the sed command to perform some changes to a text and stepped into an interesting &#8220;problem&#8221;; pattern matching the minus-hyphen (-) symbol. Assume we have the following text: something SoMeThiNg some-thing soMe_thing and we want to match all the different versions of the word with one expression (one by one). [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[37],"tags":[10,194,193],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ouW6-aw","_links":{"self":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/652"}],"collection":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/comments?post=652"}],"version-history":[{"count":6,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/652\/revisions"}],"predecessor-version":[{"id":658,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/652\/revisions\/658"}],"wp:attachment":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/media?parent=652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/categories?post=652"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/tags?post=652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}