tags:

views:

37

answers:

1

Hi...! I have a text file patterned 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.


The answers

^.\{-}\ze=

The 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 it can be said like :
"Found a text which started with any single character followed by anything and ended with "=" sign. And replace it !

+2  A: 

Hello.

:%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.

Benoit
Hi Benoit, thanks for the fast answer !,works like a charm ! very suprising me. Would you kind to explain us here the meaning of those pattern ?
brain90
Hello. ^ is start of line, .\{-} is as few characters as possible, \ze is END OF MATCHED SECTION and = is another character that is to be present afterwards, but which will not count as matched.
Benoit