You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 9-regular-expressions/15-regexp-infinite-backtracking-problem/article.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,7 +112,7 @@ First, one may notice that the regexp is a little bit strange. The quantifier `p
112
112
113
113
Indeed, the regexp is artificial. But the reason why it is slow is the same as those we saw above. So let's understand it, and then the previous example will become obvious.
114
114
115
-
What happen during the search of `pattern:(\d+)*$` in the line `subject:123456789z`?
115
+
What happened during the search of `pattern:(\d+)*$` in the line `subject:123456789z`?
116
116
117
117
1. First, the regexp engine tries to find a number `pattern:\d+`. The plus `pattern:+` is greedy by default, so it consumes all digits:
118
118
@@ -264,7 +264,9 @@ In other words:
264
264
- The lookahead `pattern:?=` looks for the maximal count `pattern:a+` from the current position.
265
265
- And then they are "consumed into the result" by the backreference `pattern:\1` (`pattern:\1` corresponds to the content of the second parentheses, that is `pattern:a+`).
266
266
267
-
There will be no backtracking, because lookahead does not backtrack. If it found like 5 times of `pattern:a+` and the further match failed, then it doesn't go back to 4.
267
+
There will be no backtracking, because lookahead does not backtrack. If, for
268
+
example, it found 5 instances of `pattern:a+` and the further match failed,
269
+
it won't go back to the 4th instance.
268
270
269
271
```smart
270
272
There's more about the relation between possessive quantifiers and lookahead in articles [Regex: Emulate Atomic Grouping (and Possessive Quantifiers) with LookAhead](http://instanceof.me/post/52245507631/regex-emulate-atomic-grouping-with-lookahead) and [Mimicking Atomic Groups](http://blog.stevenlevithan.com/archives/mimic-atomic-groups).
0 commit comments