Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim regex with metacharacters inside bracket

Tags:

regex

vim

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.

like image 708
ostler.c Avatar asked Sep 07 '11 22:09

ostler.c


People also ask

How do you include brackets in regex?

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.

Is bracket a special character in regex?

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 -.

How do you escape a metacharacter in regex?

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.


1 Answers

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.

like image 159
sidyll Avatar answered Oct 02 '22 07:10

sidyll