Ian Ian - 1 year ago 62
Perl Question

Match words in regex, but only if first letter is upper case

I have a dynamically built up largish regex, e.g shortened text example

my $regex = qr/(daisy|john|fred|june)/is;

my $test = 'Later John said blah and JOHN said ignore john .....';

while( $test =~ /($regex)/g ) {
warn $1;

# Shows all 3 matches

I would like the regex to match every occurance of 'John' OR 'JOHN' etc, but NOT if the first letter is lower case, e.g 'john' should not match.

I could repeat the regex words when building, like


But is there some interesting way of not needing to do that, and the regex only matching if the first letter is a capital ?

I could run a 2nd check outside of the regex after a case insensitive match as a possible solution, and just ignore it if match !~ /[A-Z]/ or something, but I'm interested to know if there is a way within the same regex to add an extra condition ?

Answer Source

Add a look ahead for the first letter being uppercase:

my $regex = qr/(?=[A-Z])(?i)(daisy|john|fred|june)/s

Use the case insensitive flag only after the look ahead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download