3 ms·
i never understood why non-capturing syntax requires extra work - '(?:' vs '(' - seems very backwards.
by leeoniya 13y ago
i never understood why non-capturing syntax requires extra work - '(?:' vs '(' - seems very backwards.
- pcmonk 13y agoI think it's simply because capturing groups are much more common than non-capturing groups. Even though non-capturing is "simpler", it's less common, so we default to capturing and use the more verbose syntax for non-capturing.
- teddyh 13y agoHistorical reasons. The capturing syntax came first, and when syntax for non-capturing groups was wanted, an otherwise illegal character sequence (? was chosen.
- brudgers 13y agoConsider it as a compiler flag - a regex without capture and backtracking can be represented mathematicly as a regular expression (I.e. it can be translated into a DFA). Once a regex starts capturing, its representation requires a stack or other memory structure. Or a regex might or might not be a regular expression, strictly speaking (which isn't done all that often).