问题
RegexBuddy on the tab "Debug" shows how regular expressions are executed step by step. But what exactly that steps mean? What operations are behind every step?
回答1:
The steps count is basically how many times the current position in the input was changed, which is a very good indicator of performance.
The "current position" may be at any character or between characters (including before and after the entire input).
Simplifying it, regex engines process the input by moving the current position along the input and evaluating whether the regex matches at that position. They also keep track of the position in the regex the match is up to.
I don't want to turn this answer into a regex tutorial, but... regex engines always consume as much of the input as possible while still matching. To give a simple example, given the input "12345"
and the regex .*1.*
, the regex engine will first apply .*
consuming all input leaving the position at the end of the input, fail to match a 1
, then back track by "uncomsuming" one character at a time until it finds a 1
, then continue. You can see that this would take 9 steps just to process the initial .*
.
By contrast, if the regex was [^1]*1.*
, the regex will match the "1"
in just one step.
回答2:
In RegexBuddy's debugger, a step is when the regex engine matches something, or fails to match something. Steps that match a character are indicated by all the characters matched by the regex so far which will usually be one character more than the previous step. Steps that match a position, like a word boundary, are indicated by the characters matched so far plus "ok". Steps that failed to match something are indicated by the characters matched so far plus "backtrack".
If you click on any of the matched characters in the debugger, RegexBuddy selects the token in the regular expression that matched those characters and highlights all the characters in the debugger matched by that token. If you click on an "ok" or "backtrack" indicator, RegexBuddy selects the token in the regex that matched or failed to match.
Moving the cursor with the keyboard has the same effect as clicking. Pressing the End key on the keyboard moves the cursor to the end of a step. Then pressing Arrow Up or Down moves the cursor to the previous or next step while keeping the cursor at the end of that step. By moving the cursor this way, you can easily follow how the regex engine steps through your regular expression and which characters is matches and backtracks along the way.
For more details, see these two pages in RegexBuddy's help file: https://www.regexbuddy.com/manual.html#debug https://www.regexbuddy.com/manual.html#benchmark
来源:https://stackoverflow.com/questions/41447805/what-are-steps-in-regexbuddy