Reputation: 700
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
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
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 modestackoverflow
: Match stackoverflow
in any case(?-i)
: Disable inline ignore case mode\b
: Word boundaryUpvotes: 2
Reputation: 521239
You may try using the following pattern:
\b(?!STACKOVERFLOW\b)(?i)stackoverflow\b
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