开发者

Detect words or any character after some match pattern, Regex Pattern (Vim)

I have a text file patt开发者_开发百科erned like this :

1 textA == this is textA ==
1.1 textB === this is textB ===
2 textC == this is textC ==
2.1 textD === this is textD ===
2.1.1 textE ==== this is textE ====

Whats the right regex pattern to formatting the text above:

== this is textA ==
=== this is textB ===
== this is textC ==
=== this is textD ===

I already try to perfoming this in vim :

^\w* -> this pattern just changes only textA and textB

I need to detect "." and any character or words until meet the "=" sign. Any characters behind the "=" sign will be deleted. Thanks in advance for any answers and pointers.


Solution

^.\{-}\ze=

Explanation :

^.   -> started with any single character
\{-} -> matches 0 or more of the preceding atom, as few as possible
\ze= -> Matches at any position, and sets the end of the match there: The previous char  is the last char of the whole match

In human words :

"Find and replace a text started with any single character followed by anything and ended with "=" sign.


:%s/^.\{-}\ze=//

For more details on how to design reg.exps in Vim, see :help pattern which is the most useful section help in Vim IMHO.

:help :g and :help :v could also be of interest to you. You could for example do:

:g/=/normal! 0dt=

which will emulate you typing 0dt= on each line containing an = sign.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜