Match a specific word if it contains an uppercase letter

81 Views Asked by At

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
1

There are 1 best solutions below

0
On BEST ANSWER

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

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

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