You didn't specify which engine. You mentioned PCRE, but you also tagged with Perl.
regex101 site shows that PCRE handles .*
using one operation, but that doesn't mean that one operation is faster than the operations generated by the equivalent (?:.)*
. Only benchmarking will tell, but .*
will likely be marginally faster due to less overhead.
In Perl, they compile to exactly the same regex program (as you can see below), so they will perform identically.
>perl -Mre=debug -e"'0123456789012345678901234567899' =~ /.*/"
Compiling REx ".*"
Final program:
1: STAR (3)
2: REG_ANY (0)
3: END (0)
anchored(MBOL) implicit minlen 0
Matching REx ".*" against "0123456789012345678901234567899"
0 <> <0123456789> | 1:STAR(3)
REG_ANY can match 31 times out of 2147483647...
31 <901234567899> <> | 3: END(0)
Match successful!
Freeing REx: ".*"
>perl -Mre=debug -e"'0123456789012345678901234567899' =~ /(?:.)*/"
Compiling REx "(?:.)*"
Final program:
1: STAR (3)
2: REG_ANY (0)
3: END (0)
anchored(MBOL) implicit minlen 0
Matching REx "(?:.)*" against "0123456789012345678901234567899"
0 <> <0123456789> | 1:STAR(3)
REG_ANY can match 31 times out of 2147483647...
31 <901234567899> <> | 3: END(0)
Match successful!
Freeing REx: "(?:.)*"
In both cases, the string is scanned for characters than aren't newlines, and that's it.
Note that no matter how many "steps" are taken, this cannot be done in constant time. .
doesn't match newlines (without /s
), so the regex engine must check each character it's about to match to see whether it's a newline or not.