Rick
Rick

Reputation: 561

Vim regex match multiple underscores

I'm trying to add some syntax colouring in vim for constants written in the standard uppercase form:

HELLO_WORLD
_GOOD_BYE_WORLD

when I go to http://regex101.com/ I am able to match these with the following:

/(_*[A-Z]+_*)+

but with vim it doesn't match anything.

/_ will match a single underscore but /_* will not match multiple underscores, it matches every character. After reading some of the vim regex documentation (http://vimdoc.sourceforge.net/htmldoc/pattern.html) it seems as though the underscore is used for extending matches across lines. However, all of the patterns listed in the documentation use \_ (an escaped underscore) as opposed to just the character.

How can I match words of this form?

And why does _* match every character?

Upvotes: 0

Views: 1915

Answers (3)

Tassos
Tassos

Reputation: 3298

I think \<[_A-Z]\+\> will do what you want.

Accepted answer is matching underscores and capital letters contained in lowercase words.

Upvotes: 2

Arjen Dijkstra
Arjen Dijkstra

Reputation: 1549

You can also use vim's magic option \v

/\v(_*[A-Z]+_*)+

http://vim.wikia.com/wiki/Simplifying_regular_expressions_using_magic_and_no-magic

Upvotes: 2

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42869

Vim has slightly different regex format, some key characters needs excaping, like + and (), here's your same regex formatted for vim

\(_*[A-Z]\+_*\)\+

For more info you can visit http://vimregex.com/

Upvotes: 2

Related Questions