Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: What regex can match arbitrary-length quoted string?

by AnomalousMonk (Archbishop)
on Sep 29, 2017 at 02:51 UTC ( [id://1200344]=note: print w/replies, xml ) Need Help??


in reply to What regex can match arbitrary-length quoted string?

Here's an example based on the pre-5.10  (?>pattern) atomic grouping. It should be fairly easily convertable to use the  + possessive quantifier modifier. (I'm using  'x' in place of  '"' because Windose command line and my REPL don't like double-quotes, but you get the idea.)

c:\@Work\Perl\monks>perl -wMstrict -le "print 'perl version: ', $]; print '------------'; ;; my $non_x_or_escaped = qr{ (?> [^x\\]{0,32766}) (?> (?> \\.){0,32766} (?> [^x\\]{0,32766})) }xms; ;; my $s = 'zx' . ('\x' x 1_000_000) . 'xz'; print 'length of test string: ', length $s; print substr($s, 0, 20), ' ... ', substr($s, -20); ;; print 'MATCHED' if $s =~ m{ (x (?> $non_x_or_escaped)* x) }xms; my $captured = $1; print 'length captured: ', length $captured; print substr($captured, 0, 20), ' ... ', substr($captured, -20); print '------------'; ;; $s = 'zx' . ('p' x 1_000_000) . '\x' . ('q' x 1_000_000) . 'xz'; print 'length of test string: ', length $s; print substr($s, 0, 20), ' ... ', substr($s, -20); ;; print 'MATCHED' if $s =~ m{ (x (?> $non_x_or_escaped)* x) }xms; $captured = $1; print 'length captured: ', length $captured; print substr($captured, 0, 20), ' ... ', substr($captured, -20); " perl version: 5.008009 ------------ length of test string: 2000004 zx\x\x\x\x\x\x\x\x\x ... \x\x\x\x\x\x\x\x\xxz MATCHED length captured: 2000002 x\x\x\x\x\x\x\x\x\x\ ... x\x\x\x\x\x\x\x\x\xx ------------ length of test string: 2000006 zxpppppppppppppppppp ... qqqqqqqqqqqqqqqqqqxz MATCHED length captured: 2000004 xppppppppppppppppppp ... qqqqqqqqqqqqqqqqqqqx


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1200344]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found