Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression (vim) asterisk replace

Tags:

regex

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?

like image 349
Samuel Panicucci Avatar asked Jun 06 '26 11:06

Samuel Panicucci


1 Answers

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.)

like image 150
ruakh Avatar answered Jun 08 '26 16:06

ruakh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!