Dimabytes
Dimabytes

Reputation: 536

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

Upvotes: 1

Views: 94

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627292

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.

Upvotes: 2

ChatterOne
ChatterOne

Reputation: 3541

According to your example it looks like you want a * before the last occurence of / in the line.

I'd go for recording a macro that does $F/i*^VESCj (where ^V is CTRL+V and ESC is ESC.

This means go to the end of the line, find the / backwards, enter insert mode, type the *, go back to normal mode, go down a line.

You can record this as a macro and play it back for the whole file with something like :%normal @x (if you recorded on x)

Upvotes: 0

romainl
romainl

Reputation: 196789

Vim uses its own regular expressions dialect, where:

  • the *? in .*? is meaningless,
  • grouping parentheses must be escaped with backslashes,
  • and various other differences with other dialects…

As it stands, your pattern would match:

  • a slash, \/,
  • followed by an opening parenthesis, (,
  • followed by 0 or more of any character, .*,
  • followed by a question mark, ?,
  • followed by a closing parenthesis ),
  • followed by another slash, \/,

which is nowhere to be found in your sample.

See :help perl-patterns.

Upvotes: -1

Related Questions