in reply to generate regular expression

I think I understand what you're looking for: you want a function (or module) that will take natural language queries ("2 CAC and 2 TTT") and convert them into Perl code, which may or may not include regex.

Now, my regex skills are below par, but I believe your second and third cases can be managed with single regexes. Your first case may require two.

emc

"Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
—G. Steele