vim search replace including newline

陌路散爱 提交于 2020-01-03 04:01:29

问题


I've been googling for 3 hours now without success. I have a huge file which is a concatenation of many XML files.

Thus I want to search replace all occurences of <?xml [whatever is between]<body> (including those words).

And then same for </body>[whatever is between]</html> (including those words).

The closest I came from is

:%s/<?xml \(.*\n\)\{0,180\}\/head>//g

FYI If I try this:

:%s/<?xml \(\(.*\)\+\n\)\+\/head>\n//g

I get a E363: pattern uses more memory than 'maxmempattern'. I've tried to follow this without success.


回答1:


To match any number of symbols including a newline between <?xml and <body>, you can use

:%s/<?xml \_.*<\/head>//g

The \_.* can be used to match any symbols including a newline. To match as few symbols as possible, use .\{-}: :%s/<?xml \_.\{-}<\/head>//g.

See Vim wiki, Patterns including end-of-line section:

\_.
Any character including a newline

And from the Vim regex help, 4.3 Quantifiers, Greedy and Non-Greedy section:

\{-}
matches 0 or more of the preceding atom, as few as possible

UPDATE

As far as escaping regex metacharacters is concerned, you can refer to Vim Regular Expression Special Characters: To Escape or Not To Escape help page. You can see } is missing on the list. Why? Because a regex engine is usually able to tell what kind of } it is from the context. It knows if it is preceded with \{ or not, and can parse the expression correctly. Thus, there is no reason to escape this closing brace, which keeps the pattern "clean".



来源:https://stackoverflow.com/questions/32434640/vim-search-replace-including-newline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!