0

Just looking at results from regex101, I am trying a fairly simple test:

Match /^\s+a/ (notice no g or m flags) against " b", which of course fails to match.

Just running /^\s+a/, regex101 reports 3 steps, so very efficient.

If I do /^(?:\s)+/ or /^(?>\s)+/, it suddenly jumps up to 24 steps. If I was to chalk this up to just having regex101 all of a sudden "count" each space whereas before it counted all the spaces as one step, then I'd expect the result to be 9 (start, 7 spaces, incorrect end character). But this is roughly triple that result. Is it still the case that its all just "what counts as a step" stuff, and they are actually equally fast (considering that non-capture groups and atomic groups should not be storing any information for later use), or is there actually a roughly 3x algorithmic slowdown just from using a useless group.

This is of course just a reduction of a problem I'm dealing with, where I am using non-capture groups for a practical reason (having two choices ored together), and seeing the number of steps explode well beyond the 2x I'd expect from checking 2 kinds of things vs. one kind of thing. I narrowed it down to this non-capture group thing.

Quick Edit: For whatever reason, regex101 will report "0 steps" in the as-you-type view, but if you click on the debugger, it will show the actual number of steps:

enter image description here

  • There is no actual problem here. All these patterns are equally efficient. – Wiktor Stribiżew Dec 02 '20 at 21:29
  • If you click on the debugger, it shows you the actual steps. For whatever reason regex101 (incorrectly?) reports zero steps in the "instant view", but the correct number of steps in the step-by-step debugger. I'll add a note in the question itself. – Francisco Ryan Tolmasky I Dec 02 '20 at 21:31
  • Yes, I noticed that and deleted my comment. If you click on the Play button in the debugger, you can see all the steps. For some reason, it's repeatedly backtracking in the group. – Barmar Dec 02 '20 at 21:32
  • I've had to move my question here since this one was marked as a duplicate despite the duplicate being about a very different set of circumstances: https://stackoverflow.com/questions/65116542/why-do-atomic-groups-and-non-capturing-seem-to-add-many-steps – Francisco Ryan Tolmasky I Dec 02 '20 at 21:40

0 Answers0