In vimscript I can regexp-match stuff with matchstr() and matchlist()
But what if I have to append to the pattern a user-provided string?
How can I quote meta characters in that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think there’s anything that does that, but you could use a magicness modifier which might be good enough. Let’s say you have the following expression:
if
wordis a user-supplied string, then (if I understand correctly) you’d like to ignore all special modifiers in it. To do that, use “very nomagic mode”. By putting a “\V” in front of the pattern, you make the pattern behave almost like a literal string. Afterwards, put “\m” to restore normal behaviour. In this case:Note, however, that this is not exactly what you’re asking for. If the user enters
foo(*bar), it will work just fine, but if it’sfoo(\*bar), it won’t, because the\*pair is being interpreted as a star modifier. If you don’t know about these modes, try:help /\Vto get a better idea of how magicness works.