There's no easy way to do this. You could modify the regex engine, or you could modify your regex to check for the appropriate conditions. Even with a regex parser, it might be very tricky to do the latter automatically.

Here's the version of /a\d+b/ with the checks added:

# /a\d+b/ while (<DATA>) { local our $incomplete; my $match = / a (?:$(?{$incomplete=1})(?!)|(?(?{$incomplete})(?!)) \d+ (?:$(?{$incomplete=1})(?!)|(?(?{$incomplete})(?!)) b ) ) /x; my $rv = $match ? "match" : $incomplete ? "incomplete" : "no match"; chomp; printf("%-10s %s\n", $_, $rv); } __DATA__ a123b a a1 a123 a123c a123ca123b a123ca123 a123ca123c
a123b match a incomplete a1 incomplete a123 incomplete a123c no match a123ca123b match a123ca123 incomplete a123ca123c no match

I recommend that you write a tokenizer and parser. If your language doesn't allow line breaks to happen in the middle of a token, the only time you need to read more data is when you're at the end of the buffer when the parser requests a new token.

my $ws = qr/\s+/; sub get_token { my ($self) = @_; for ($self->{buf}) { s/^$ws//; if (length() == 0) { my $fh = $self->{fh}; return [ TOK_EOF ] if eof($fh); $_ .= <$fh>; redo; } s/^([a-zA-Z][a-zA-Z0-9_]*)// && return [ TOK_IDENT, $1 ]; ... } }

If some tokens can contain line breaks, handle those cases specially.


In reply to Re: Did regex match fail because of "end of string"? by ikegami
in thread Did regex match fail because of "end of string"? by moritz

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.