(Note: Title doesn’t seem to clear — if someone can rephrase this I’m all for it!)
Given this regex: (.*_e\.txt), which matches some filenames, I need to add some other single character suffixes in addition to the e. Should I choose a character class or should I use an alternation for this? (Or does it really matter??)
That is, which of the following two seems “better”, and why:
a) (.*(e|f|x)\.txt), or
b) (.*[efx]\.txt)
Use
[efx]– that’s exactly what character classes are designed for: to match one of the included characters. Therefore it’s also the most readable and shortest solution.I don’t know if it’s faster, but I would be very much surprised if it wasn’t. It definitely won’t be slower.
My reasoning (without ever having written a regex engine, so this is pure conjecture):
The regex token
[abc]will be applied in a single step of the regex engine: “Is the next character one ofa,b, orc?”(a|b|c)however tells the regex engine toa. If so, success. If not:b. If so, success. If not:c. If so, success. If not: