Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: regular expression to match up to a string

Tags:

regex

vim

What regular expression in Vim will match all characters up to no. in the below sting?

foo, bar, and baz, no. 13, qux

In other words the match must be foo, bar, and baz,

I'm new with regular expressions, did my research here on Stack Overflow, and one suggestion is .+?(?= no.). But it seems to work only with the regular (perl?) flavour of regular expressions, not in Vim.

Please, help.

Maybe there's a simpler solution to what I'm trying to achieve. My ultimate goal is to put foo, bar, and baz in curly brackets, and I planned to to a global substitution command involving regular expressions.

like image 438
Mihai Nagy Avatar asked Oct 28 '25 07:10

Mihai Nagy


1 Answers

this regex in vim should do

.*\zeno

e.g. you do this on your line:

s/.*\zeno//

that line would be changed into:

no. 13, qux

EDIT

just saw your "ultimate goal", you can just use the regex:

:s/.*\zeno/{&}

if you don't want to wrap the comma:

:s/.*\ze, no/{&}
like image 189
Kent Avatar answered Oct 29 '25 22:10

Kent



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!