user697911
user697911

Reputation: 10531

How to match the white space between two strings in vim?

4250682 orderNumber: 60360981400                         orderID:4250682 

There could be white spaces or tab between "60360981400" and "orderID". How to match those spaces and replace it with only one space?

orderNumber:\d\+.*orderID 

This can match the pattern. But how to replace the '.*' with one space?

Upvotes: 0

Views: 326

Answers (2)

bimlas
bimlas

Reputation: 2597

If you want to match only a part of the regex expression, you can use \zs (start of match) and \ze (end of match) expressions: the matching part before \zs and after \ze will be discarded, thus the inner match will be replaced:

s/orderNumber: \d\+\zs.*\zeorderID/ /

For additional details, see :help \zs.

Upvotes: 3

Pablo Pretzel
Pablo Pretzel

Reputation: 158

In order to replace all instances of consecutive whitespaces with one space, you can use the whitespace metacharacter \s, as in :s/\s\+/ /g.

Or look into grouping and backreferences: :s/\(orderNumber: \d\+\).*\(orderID\)/\1 \2/

http://vimregex.com/ will explain all of the above and a lot more.

Upvotes: 0

Related Questions