3 ms·
Or how about this: a single regex, but with groups that you can walk like a tree: (((a)|(b))|((c)|(d))) | (((e)|(f))|((g)|(h))) The number of groups will be O
by adamtj 13y ago
Or how about this: a single regex, but with groups that you can walk like a tree:
(((a)|(b))|((c)|(d))) | (((e)|(f))|((g)|(h)))
The number of groups will be O(n * log n). So 10-20 times the number of urls at most. It's certainly not quadratic.
The groups will in depth-first order. It's slightly complicated by the fact that the regexes contain capturing groups themselves, but you can precompute an offset table that accounts for them.
Or give each tree-group a name:
(?<0>(?<00>(?<000>a)|(?<001>b))|(?<01>(?<010>c)|(?<011>d))) | (?<1>(?<10>(?<100>e)|(?<101>f))|(?<11>(?<110>g)|(?<111>h)))
If you use names like that, walking the tree is easy. Just concatenate "0" or "1" to the current node node to find the child nodes. Also, the final matching group name is a binary number corresponding to matching route's index into the list of routes. For example, url "e" has the name "100". That's a binary 4, which is url "e"'s position in the list.