while () { if (/\b keyword \s+ (\w+)/ and exists $keywords{ $1 }) { # .. do something with the token in $1 } }