Character classes don't involve backtracking as this example shows.
my $string = 'bet';
use re qw(debug);
my $rx1 = qr/b(?:a|e)t/;
$string =~ /$rx1/;
my $rx2 = qr/b[ae]t/;
$string =~ /$rx2/;
output of
$rx1 which shows the branch
Compiling REx `b(?:a|e)t'
size 12 Got 100 bytes for offset annotations.
first at 1
1: EXACT <b>(3)
3: BRANCH(6)
4: EXACT <a>(10)
6: BRANCH(9)
7: EXACT <e>(10)
9: TAIL(10)
10: EXACT <t>(12)
12: END(0)
anchored "b" at 0 (checking anchored) minlen 3
Offsets: [12]
1[1] 0[0] 4[1] 5[1] 0[0] 6[1] 7[1] 0[0] 7[0] 9[1] 0[0] 10[0]
Guessing start of match, REx "b(?:a|e)t" against "bet"...
Found anchored substr "b" at offset 0...
Guessed: match at offset 0
Matching REx "b(?:a|e)t" against "bet"
Setting an EVAL scope, savestack=7
0 <> <bet> | 1: EXACT <b>
1 <b> <et> | 3: BRANCH
Setting an EVAL scope, savestack=13
1 <b> <et> | 4: EXACT <a>
failed...
1 <b> <et> | 7: EXACT <e>
2 <be> <t> | 10: EXACT <t>
3 <bet> <> | 12: END
Match successful!
output of
$rx2 where there is no branching.
Compiling REx `b[ae]t'
size 16 Got 132 bytes for offset annotations.
first at 1
1: EXACT <b>(3)
3: ANYOF[ae](14)
14: EXACT <t>(16)
16: END(0)
anchored "b" at 0 (checking anchored) minlen 3
Offsets: [16]
1[1] 0[0] 2[4] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[
+0] 6[1] 0[0] 7[0]
Guessing start of match, REx "b[ae]t" against "bet"...
Found anchored substr "b" at offset 0...
Guessed: match at offset 0
Matching REx "b[ae]t" against "bet"
Setting an EVAL scope, savestack=9
0 <> <bet> | 1: EXACT <b>
1 <b> <et> | 3: ANYOF[ae]
2 <be> <t> | 14: EXACT <t>
3 <bet> <> | 16: END
Match successful!
Freeing REx: `"b(?:a|e)t"'
Freeing REx: `"b[ae]t"'
These examples were run under perl 5.8.8. 5.9.5 introduced
trie optimization which gets rid of the branch. Output of
$rx1 under 5.10.0
Compiling REx "b(?:a|e)t"
Final program:
1: EXACT <b> (3)
3: TRIE-EXACT[ae] (10)
<a>
<e>
10: EXACT <t> (12)
12: END (0)
anchored "b" at 0 (checking anchored) minlen 3
Guessing start of match in sv for REx "b(?:a|e)t" against "bet"
Found anchored substr "b" at offset 0...
Guessed: match at offset 0
Matching REx "b(?:a|e)t" against "bet"
0 <> <bet> | 1:EXACT <b>(3)
1 <b> <et> | 3:TRIE-EXACT[ae](10)
1 <b> <et> | State: 1 Accepted: 0 Charid:
+2 CP: 65 After State: 3
2 <be> <t> | State: 3 Accepted: 1 Charid:
+2 CP: 0 After State: 0
got 1 possible matches
only one match left: #2 <e>
2 <be> <t> | 10:EXACT <t>(12)
3 <bet> <> | 12:END(0)
Match successful!
Freeing REx: "b(?:a|e)t"