Hacker News new | ask | show | jobs
by steffann 3034 days ago
I get the suspicion that the writer doesn't understand the ?, * and + operators...
2 comments

There is a problem here in that different regex libraries have different semantics for these.

I checked the manual for PCRE (man pcrepattern), and it says that ? has both the meaning of {0,1} (zero or one repetition), as well as turning * and + into non-greedy variants if directly following them.

Similarly, + usually has the meaning of {1,} (at least once) but can also quantify * and + to prevent backtracking.

For an engine whose semantics differ from PCRE, non-greedy matching or backtracking might not even make sense, if the matching is implemented differently (e.g. using finite automata that don't backtrack).

a correction would be helpful.

   /x?/ one or none x
   /x+/ one or more x
   /x*/ none or more x
with variable operators it's more complicated. In /.+/ the operator is repeated, not the first match as with /(.)\1*/

Also, there are extensions in various implementations that are not in fact a regular, so no finite automata.