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

Re: how to write multi-line regex

by shmem (Canon)
on Mar 24, 2014 at 07:48 UTC ( #1079492=note: print w/ replies, xml ) Need Help??

in reply to how to write multi-line regex

Your input looks like a perfect candidate for "paragraph mode" using the switches -n and -00 (see perlrun):

#!/usr/bin/perl -n00 /^TEXT;\nLAYER 13[1-7];\nTEXTTYPE 0;.+STRING (.+?);/s and print $1,"\n +";

The //s modifier is necessary, since without it, the "." character would not match a newline.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Comment on Re: how to write multi-line regex
Download Code
Re^2: how to write multi-line regex
by kcott (Abbot) on Mar 25, 2014 at 04:15 UTC

    As well as the 's' modifier which, as you correctly state, is needed for '.' to match a newline; you'll also need the 'm' modifier for '^' to match the start of each line in a multi-line string (which paragraph mode [-00] will give you). Without this modifier, '^' will match only once at the start of the string. ("perlre: Modifiers" and "perlre: Metacharacters" have details of both of those.)

    The following two points are really more a comment on the way the OP posted the sample data than on your solution.

    The start of the regex (/^TEXT ...) assumes TEXT starts at the beginning of a line. While I agree that is likely to be the case for the real data, the HTML source for the posted data suggests otherwise:

    <p> ... <br /> ... <br /> TEXT; <br /> LAYER 133;

    The two '\n's in the regex suffer from a similar problem. While it's likely that the real data has lines that are only separated by a single newline, the posted data has additional whitespace before and after various lines.

    -- Ken

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-08-01 02:03 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (256 votes), past polls