Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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"


In reply to Re: Why would one want in a regex a class with only a single entry? by hipowls
in thread Why would one want in a regex a class with only a single entry? by ack

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found