Regex kleene star question

reading again the book since it’s complete and stumbled on

A regular expression a*a will match one or more letters a. However, a*a in PEG will never match anything…

On page 44.

TIL! I assumed regex matched like PEG but turns out it’s not the case.
Is it due to backtracking in regexp? Where is this behaviour documented?

Thanks!

2 Likes

PEG backtracks only at the points where the alternative (/) operator is used. You can see this behaviour from the code here:

If this.parse(source) fails then the other parser.parse(source) is retried with the same source.

Regexes, on the other hand, backtrack in general when a match is not found.
The following regex example might be useful: https://riptutorial.com/regex/example/3179/what-causes-backtracking.

I’ve never implemented a regex engine, so I have only an intuitive understanding of it.

1 Like

Thanks!

Yeah, PEG backtracking wasn’t surprising for me but regexp’s was; but only the ones with O(n) guarantees like RE2(tested on play.golang.org). So i was wondering how is that implemented since it’s working allegedly without backtracking…

Not expecting the answer from you since it’s off topic, just thinking out loud :slight_smile:

1 Like