Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: how to write multi-line regex

by shmem (Chancellor)
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'

Replies are listed 'Best First'.
Re^2: how to write multi-line regex
by kcott (Chancellor) 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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-06-21 20:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (119 votes). Check out past polls.