简体   繁体   中英

Regex to detect if words are part of the string and next word is not capitalized

I am looking for a regex that would match a specific word or words which are part of the given string with a restriction - if there is a word after pattern this word should not be capitalized. Let's assume that the words are ' Base Case ', so here are some examples

  • Final Base Case - should match
  • Final Base Case financial - should match
  • Final Base Case Financial - should not match (the next word 'Financial' is capitalized)
  • Final Base Cases - should not match ('Case' and 'Cases' are not matched)

I use the following regex to determine if my word/words are part of the string

\bBase Case(?!\w)

Can someone please help me modify my regex expression to add restriction for the next capitalized word?

You need to check for two cases after the search pattern:

  1. end of string ( $ ); or
  2. another word, which doesn't begin with a capital letter ( \s+[^AZ\s] )

You can do that with this regex:

\bBase Case(?=$|\s+[^A-Z\s])

Note that since the second half of the lookahead asserts a space before the next word, it prevents Base Cases or similar from matching.

Demo on regex101

In a most generic case, you may use

\bBase\s+Case\b(?!\W*\p{Lu})

See the regex demo .

Details

  • \b - a word boundary
  • Base\s+Case - Base , 1+ whitespaces, Case
  • \b - a word boundary
  • (?!\W*\p{Lu}) - a negative lookahead that fails the match if there are 0 or more non-word chars followed with any Unicode uppercase letter immediately to the right of the current location.

If there is only whitespace expected between the word and the uppercase letter, replace \W with \s .

C# usage:

var results = Regex.Matches(text, @"\bBase\s+Case\b(?!\W*\p{Lu})")
    .Cast<Match>()
    .Select(m => m.Value)
    .ToList();

Or, just to check if it exists in each string:

var texts = new List<string> {"Final Base Case", "Final Base Case financial", "Final Base Case Financial", "Final Base Cases"};
foreach (var text in texts) {
    Console.WriteLine("{0}: {1}", text, Regex.IsMatch(text, @"\bBase\s+Case\b(?!\W*\p{Lu})"));
}

See C# demo . Output:

Final Base Case: True
Final Base Case financial: True
Final Base Case Financial: False
Final Base Cases: False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM