Reputation: 1897
I'm trying to understand how VIM uses 'pattern' argument to 'matchstr' function.
I tried creating a pattern that matches either 'a' or 'b' but I'm unable to do this.
Here is what I tried:
:echo matchstr("ab", "a|b")
:echo matchstr("ab", "a\|b")
:echo matchstr("ab", "(a|b)")
:echo matchstr("ab", "(a|b)") :echo matchstr("ab", "(a\|b)")
Note: 'set magic?' shows 'magic'
Upvotes: 0
Views: 1378
Reputation: 8248
Vim uses a regex dialect, in which by default you need to escape special letters, if you need their regex feature. E.g. for OR you need to write \|
and not like in perl regexes |
This applies e.g. to the multi atom +
and the OR atom |
. (This can be changed by the regex atom \v
which provides a more perl like regex dialect, see :h /\v
)
Now you are using double quotes in your expression. When using double quotes, Vim will parse special characters and therefore remove one backslash, before the regex engine even sees them. Therefore, you need to either double your backslashes or use single quotes. This is explained at :h expr-quote
Upvotes: 5