original.roland
original.roland

Reputation: 700

Regex to match not full uppercase representation of a specific word

There's a word, let's say stackoverflow. I would like to find EVERY instance of this word, where it's NOT written in FULL UPPERCASE.

So, example I'm looking for:

... etc

But I don't want to include:

Is it a problem which can be solved with pure regex? Regex engine is not specified, feel free to use whatever supports your feature needs.

Upvotes: 0

Views: 129

Answers (2)

anubhava
anubhava

Reputation: 785146

Here is a regex that would work in other flavors as well e.g. Javascript or python:

\b(?![A-Z]+\b)[sS][tT][aA][cC][kK][oO][vV][eE][rR][fF][lL][oO][wW]\b

RegEx Demo

If you're using PCRE or Java then you can use:

\b(?![A-Z]+\b)(?i)stackoverflow(?-i)\b

RegEx Details:

  • \b: Word boundary
  • (?![A-Z]+\b): Negative lookahead to fail the match of we have all upper case letter before matching a word boundary
  • (?i): Enable inline ignore case mode
  • stackoverflow: Match stackoverflow in any case
  • (?-i): Disable inline ignore case mode
  • \b: Word boundary

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521239

You may try using the following pattern:

\b(?!STACKOVERFLOW\b)(?i)stackoverflow\b

Demo

This pattern says to:

\b                   match a word boundary
(?!STACKOVERFLOW\b)  assert that the word is not STACKOVERFLOW in all caps
(?i)stackoverflow    then match "stackoverflow" with each letter in any case
\b                   closing word boundary

This answer assumes that your regex engine supports the (?i) case insensitive flag.

Another approach to your problem would be to simply do two separate matches, one for STACKOVERFLOW and the other a case insensitive search.

Upvotes: 3

Related Questions