Using alternation or character class for single character matching?

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 of a, b, or c?”

(a|b|c) however tells the regex engine to

  • remember the current position in the string for backtracking, if necessary
  • check if it’s possible to match a. If so, success. If not:
  • check if it’s possible to match b. If so, success. If not:
  • check if it’s possible to match c. If so, success. If not:
  • give up.

Leave a Comment