in reply to how can I combine these expressions?

Well, I had trouble figuring out exactly what you needed, but a rough first (untested) try was simply using alternation. I also replaced the character class with a variable to make it a bit more legible.
$valid = "(?:[_a-zA-Z]\w*)"; $str =~ s/(^|[^\%](?:\%\%)*)\%($valid|\{$valid\}))/"$1".$env->{$2}/ge;
If you could post some working code with sample data, I could work on something better. I tried playing with your code and couldn't really get a good feel for what you were doing.

Cheers,
Ovid