0

What would be the following vim regex to get the word term in:

hello my*term*here 

My thought was:

*\zs[^*]\ze*

But it seems like there are some things I need to escape here that aren't working. The following works, but I really have no idea why -- why does the * not need to be escaped the first time but it does the second?

/*\zs[^*]\+\ze\*
2

You do need to escape the * (with \*) since a * by itself means "zero or more repetitions of the preceding item."

When the * appears as the first character of the regular expression, that's an exception, since there's no preceding item, it will match a literal asterisk instead.

Note that inside a [...] character class, you typically don't need to escape characters with \, since metacharacters lose their special properties inside them.

The most correct (assuming the default setting for 'magic') would be:

/\*\zs[^*]\+\ze\*
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.