New User Q: MultiLine Match 
Author Message
 New User Q: MultiLine Match

Hi All,

How can I match the following string:

1
---string---

I've read the Perl FAQ on multiline matching and am still not sure how
to do it.

Here is an attempt

$/ = '';
  while ( <> ) {
        while ( /^1 /gm )
            print "Found a match  $.\n";
        }
    }

Also, since this pattern that I want to match exist withing a file that
I am manipulating, do I have to reset  the [INPUT_RECORD_SEPARATOR]  $/
back to $/ = \n ?

thanks

kevin newman



Wed, 18 Jun 1902 08:00:00 GMT  
 New User Q: MultiLine Match

Quote:
>Hi All,

>How can I match the following string:

>1
>---string---

>I've read the Perl FAQ on multiline matching and am still not sure how
>to do it.

>Here is an attempt

>$/ = '';

If you do that, looping over <> below doesn't make sense.  The first
<> will read the whole file, there's nothing left to loop over.

Quote:
>  while ( <> ) {
>        while ( /^1 /gm )

Here you're trying to match a line that begins with 1, followed by a
blank.  The blank doesn't seem to be in the data you show above, but
who knows what happens to trailing blanks during news transfer.

You should look for something like m/^1\n---/gm to loop over pairs of
lines.

Quote:
>            print "Found a match  $.\n";
>        }
>    }

The $. variable isn't going to do you much good here, since you're not
reading lines.  You'll have to use your own counter.

Quote:
>Also, since this pattern that I want to match exist withing a file that
>I am manipulating, do I have to reset  the [INPUT_RECORD_SEPARATOR]  $/
>back to $/ = \n ?

I don't see what you're getting at here.  After the first read action
on your file you have everything in one string.  Changing $/ after the
fact won't change that.

Anno



Wed, 18 Jun 1902 08:00:00 GMT  
 New User Q: MultiLine Match
[posted & mailed]

Quote:


> >$/ = '';

> If you do that, looping over <> below doesn't make sense.  The first
> <> will read the whole file, there's nothing left to loop over.

That really depends on whether or not there are blank lines in the file.

$/ = "" means paragraph mode.  Perhaps you are thinking of $/ = undef.

--
Rick Delaney



Wed, 18 Jun 1902 08:00:00 GMT  
 New User Q: MultiLine Match

Quote:
>[posted & mailed]



>> >$/ = '';

>> If you do that, looping over <> below doesn't make sense.  The first
>> <> will read the whole file, there's nothing left to loop over.

>That really depends on whether or not there are blank lines in the file.

>$/ = "" means paragraph mode.  Perhaps you are thinking of $/ = undef.

Dear me, yes, I was thinking of undef.  Of course, in paragraph mode
the loop makes sense.  Sorry for the confusion.

Anno



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. New User Q:Tricky multiline Join

2. multiline, multi pattern match

3. multiline regex matching

4. Multiline regexps with matches

5. bug in anchored, multiline pattern match

6. pattern matching and multilines (sort of)

7. multiline matching fun

8. Matching and removing multiline block in perl 1-liner

9. matching multiline C constants

10. multiline match in perl 5

11. Multiline Matching

12. Multiline pattern matching with command line invocation

 

 
Powered by phpBB® Forum Software