You're right. One possible optimization is to change the regex for attributes not to match type (with negative lookahead), and then use the * star. I don't know if that's actually faster, but I'm sure the OP will benchmark it if speed is really important.