Search code examples
regexvimvi

Vim. Add * to the end of each word between slashes


I have text:

alpha/beta/gamma
alpbeta/gamma
alpha/beta/gamma
This is an example.  Test1,Test2,Test3
alpha/beta/gamma
This is an example.
Test1,Test2,Test3

i want to add * to the end of each word between slashes (/), but vim don't found nothing by my pattern...

my command:

:%s/\/(.*?)\//*/g

result I want:

alpha/beta*/gamma
alpbeta/gamma
alpha/beta*/gamma
This is an example.  Test1,Test2,Test3
alpha/beta*/gamma
This is an example.
Test1,Test2,Test3

Solution

  • You can use

    :%s/\/[[:alpha:]]\+\ze\//&*/g
    

    Or even

    :%s/\/[^\/]*\ze\//&*/g
    

    Here, the pattern is \/[[:alpha:]]\+\ze\/:

    • \/[[:alpha:]]\+ - The consuming part: / and then one or more letters
    • [^\/]* - zero or more chars other than /
    • \ze\/ - end of the text consumed and then a / char must follow (as if it were a (?=\/) positive lookahead in common NFA regular expressions).

    The replacement is & that stands for the whole match value and a * char.

    The g flag replaces all occurrences.