Robert Dodier
Robert Dodier

Reputation: 17576

Regex for matching a string with any capitalization except for a specific capitalization

I'm trying to construct a regex to match a string in any capitalization except for a specific capitalization. For example, given foobar, I would like to match Foobar, fooBar, FOOBAR, etc., but not foobar.

A workaround which has the desired result is to replace all foobar (case sensitive) by a different string, say ABC, and then do a case-insensitive search for foobar -- then it will match all the variants. But I'd rather have a way to do it without modifying the text to be searched.

I'm working with Vim, but if anyone knows a way to do this is any regex flavor, I'm sure I could figure out how to use it.

Upvotes: 0

Views: 51

Answers (1)

Andreas Louv
Andreas Louv

Reputation: 47099

For flavors that supports modifying the regex flags, one can use the following:

(?!foobar)(?i)foobar

That uses a negative case sensitive lookahead, and then swaps to incase-sensitive mode, to match the string again.

Vim does not support toggling ignorecase midway though a match, as \c and \C will apply for the whole match.

FWIW then you can do something as stupid as:

/\C\(foobar\)\@![fF][oO][oO][bB][aA][rR]

Upvotes: 1

Related Questions