Saturday, January 25, 2014

Re: Replacing a string which is in a line and not in the beginning?

Niels Kobschätzki-2 wrote
> I have a file that looks like this:
> - This is a te- st
> - foo bar
>
> And I want to replace all occurrences of "- " when they are not at the
> beginning of the line. So the above mentioned file should look like:
> - This is a test
> - foo bar
>
> I tried to find something and it seems that in "normal" regex I would
> have to do something like ^.+(- .) or ^.?
> <pattern>
> but those don't work.
> What do I have to do to get the replacements I want?

I'm fairly new to Vim myself, but I just tried this.

I entered this into a new buffer --

- This is a te- st
- foo bar
- This is another - test I mean

and ran this from the command line (using "x" as the replacement text) --

:%s/\v^(.+)\- /\1x/g

and I got this:

- This is a texst
- foo bar
- This is another xtest I mean

Note that I used "\v" (very magic) to make Vim use regular expressions that
the rest of the world understands. :-)



--
View this message in context: http://vim.1045645.n5.nabble.com/Replacing-a-string-which-is-in-a-line-and-not-in-the-beginning-tp5720146p5720148.html
Sent from the Vim - General mailing list archive at Nabble.com.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment