I have this regex:
(\d?)[A-Za-z](?=(\d?)(.*))(?=(.* ){5})
It would be used by me for Chess FEN strings.
I wonder if regex [A-Za-z]
is faster than [RNBQKrnbqk]
? I only need to check the given letters (but no other letters will appear).
My thoughts are:
[A-Za-z]
- the regex engine can match if a char is65 <= c <= 90
or97 <= c <= 122
. Worst case is 4 comparisons.[RNBQKrnbqk]
- the engine checks if the inspected char equals every given character in the group. Worst case is 10 comparisons.
Am I understanding regex correctly?
[A-Za-z]
and[RNBQKrnbqk]
wont be noticeable. I'd take a look at your lookaheads (?=(\d?)(.*)) can match 0 or more of any character (\d?) matching 0 or 1 number so if there is no number .* matches. Also the {5} in (?=(.* ){5}) is redundant as .* can match nothing to the end of the input so {5} can match 5 lots of nothing \$\endgroup\$ – JGNI Aug 2 '18 at 9:31