Samuel Panicucci
Samuel Panicucci

Reputation: 97

regular expression (vim) asterisk replace

In this simple code:

char **s = NULL;
char **s1 = NULL;

I want to replace "**s" with "*s",

the result should be:

char *s = NULL;
char **s1 = NULL; 

but if I try:

%s/\<\*\*s\>/\*s/g

replace failed. If try this:

%s/\*\*s/\*s/g

the result is:

char *s = NULL;
char *s1 = NULL; 

replace succeded, but also "**s1" is replaced

Why the first method FAIL?

Upvotes: 3

Views: 5640

Answers (1)

ruakh
ruakh

Reputation: 183251

In vim regular expressions, \< means a word boundary. There's no word boundary between the space and the asterisk — neither one is part of a word — so \<\* doesn't match. You need this:

%s/\*\*s\>/\*s/g

which addresses that issue, while still retaining the word-boundary after s (so as not to match *s1). (\< and \> are frequently used in pairs to match a whole word, but they don't have to be. Either can be used without the other.)

Upvotes: 2

Related Questions