regexpcrepcre2

Match a specific word if it contains an uppercase letter


What's the PCRE2 regular expression syntax to match a specific word if and only if it contains at least one uppercase letter? I can currently match both conditions individually (^foobar$ or [A-Z]+), but don't know how to combine as a compound conditional. Expected results for testing "foobar":

✅ Foobar    ✅ fooBAR    ❌ foobar    ❌ Foobars   ❌ sFoobar

Solution

  • Positive lookahead assertions (?=...) and local options (?i:...) can be used:

    ^(?=.*[A-Z])(?i:foobar)$
    

    Tests: https://regex101.com/r/JdQlaQ/1