in reply to Strange regex result

The .* (?<= $tail ) part (assuming that the \) is an error, you already have one in $tail) means "grab as many characters as possible, and then backtrack until you find the string 5) before the current position".

So the paren group 1 matches (1, 2), the .* matches ,3,(4,5), and the regex engine is happy to have found a match and buys demerphq and dave_the_m a beer - or so I hope.

If you want to match (4, 5), omit the .*.