How can you search for a metacharacter or another character in a vim regular expression?
Example: (I would like this to match the metacharacter '\w' or the character '-' 1 or more times
[-\w]\+
But that regex does not work like I would hope it would. It only matches -, w and . I have tried to escape the '\' but that doesn't work.
I have looked through the vim documentation, but there is no example of this.
By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex. Only parentheses can be used for grouping.
In most regex flavors, the only special characters or metacharacters inside a character class are the closing bracket ], the backslash \, the caret ^, and the hyphen -.
To match any of the metacharacters literally, one needs to escape these characters using a backslash ( \ ) to suppress their special meaning. Similarly, ^ and $ are anchors that are also considered regex metacharacters.
Inside a collection, you have to use special character classes. The following expression is equivalent to yours:
[-_[:alnum:]]\+
Check :h [:alnum:]
(and subsequent lines) for a complete list on supported classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With