{"id":1445,"date":"2020-01-03T09:21:50","date_gmt":"2020-01-03T15:21:50","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1445"},"modified":"2020-04-12T12:03:05","modified_gmt":"2020-04-12T17:03:05","slug":"regex-search-lines-add-quotes","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/regex-search-lines-add-quotes\/","title":{"rendered":"Regex Search Lines Add Quotes"},"content":{"rendered":"

Regex to search a whole file and if the line contains the search word, add beginning and ending quotes. I in this case doing it vi(m).<\/p>\n

This works to add beginning and ending quotes to ALL lines in the file<\/p>\n

**:%s\/^(.*)$\/\\\\1\/**<\/code><\/pre>\n

or simpler<\/p>\n

**:%s\/.*\/&**<\/code><\/pre>\n

Explanation:<\/h3>\n

By default, a pattern is interpreted as the largest possible match, so .* is interpreted as the whole line, with no need for ^ and $. And while (\u2026) can be useful in selecting a part of a pattern, it is not needed for the whole pattern, which is represented by & in the substitution. And the final \/ in a search or substitution is not needed unless something else follows<\/p>\n

However we do NOT want all lines in the file only the lines containing the search word.<\/p>\n

This works. I am not sure if this is reliable without using anchors ^ and $ but it seems to work in my limited test.<\/p>\n

**:%s\/.*mysearchword.*\/&**<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

Regex to search a whole file and if the line contains the search word, add beginning and ending quotes. I<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[],"class_list":["post-1445","post","type-post","status-publish","format-standard","hentry","category-regex"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1445"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1445\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}