The regex is fine, but the modifier is not. You're using the
/g modifier in scalar context. When checking the match at line 19, the modifier remembers the position when the match occurred, and the matching at line 26 tries to start from there - and fails. Remove the modifiers and everything will work.