Which is faster for regex: [abc] or (?:a|b|c)

Viewed 40

Pretty much what it is in the title. Is using the square brackets ([]) a faster selection of alternative matches than using the pipe (|)? I'm writing a function that assembles a list of regex strings, which if any of the regex strings match, I would want to return a successful match object, but I'm not sure how much of a performance jump I could expect from isolating the single character regex strings, for instance \d or a and using [] instead of piping them.

For example, [a\d]+ versus (?:a|\d)+. I'm not overly familiar with the performance of the regex engine for specific patterns, so any tips would be welcome

Note: I would always be wanting to use the non-capturing group, so assume its not just a|b, but instead always (?:a|b)

0 Answers
Related