As far as I know, you can't match a regex on a filehandle -- you have to have the entire string ready at the time of match. The regex engine won't "ask for more data" to become available.

You could build a plain old DFA. A DFA implementation is really simple, but the painful part is building the transition table. A DFA is a very low-level machine, so you have to end up writing very low-level code for it. Plus, you generally read one character at a time, which isn't very efficient.

What would be nicer is to have a state machine that reads one token at a time. So you should look at a lexer. A lexer will take a data stream (filehandle), and tokenize is as it comes in. This has the advantage of still processing the data as soon as it's available, but the token processing being much higher-level than a character-by-character DFA. Look at Parse::Lex (documentation en francais) and Parse::Flex.

blokhead


In reply to Re: regex for a socket stream by blokhead
in thread regex for a socket stream by johnnywang

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.