pattern matching across two lines 
Author Message
 pattern matching across two lines

I have text as follows:

foo^M
bar

in several files. I need to replace this with:

foobar

How can I match patterns across two lines?

Thanks,

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 pattern matching across two lines

Quote:

> How can I match patterns across two lines?

Hello, the simplest way I see is:
while(<>) {
    if(/patternA/) {
        $_ = <>;
        if(/patternB/) {
             #You have detected the pattern accross two lines
        }
    }

Quote:
}

I hope this helps,
Federico


Wed, 18 Jun 1902 08:00:00 GMT  
 pattern matching across two lines


Quote:
> I have text as follows:

> foo^M
> bar

> in several files. I need to replace this with:

> foobar

> How can I match patterns across two lines?

http://www.oreilly.com/catalog/regex/
Mastering Regular Expressions ( Chapter 7 )

open(FH,$ARGV[0]);
local($/);
$str=<FH>;
close(FH);

$str =~ s/foo\r\nbar/foobar/gsm;
print $str;

--
=============================
Richard More
http://www.richmore.com/

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 pattern matching across two lines

Quote:

> open(FH,$ARGV[0]);

It's better to check and see whether the open succeeded or not ..

Quote:
> local($/);

why do you localize $_ when you don't use it?

Quote:
> $str=<FH>;
> close(FH);

> $str =~ s/foo\r\nbar/foobar/gsm;

Hmmm .. the /s modifier affects the behaviour of the dor '.' inside a
regexp making it match newlines. And the /m modifier affects the
behaviour of '^' and '$' making them match within a string.

You don't have a '.', '^' or '$' in your regexp, so why are you using
/sm?

--Ala



Wed, 18 Jun 1902 08:00:00 GMT  
 pattern matching across two lines


Quote:

> > How can I match patterns across two lines?

> Hello, the simplest way I see is:
> while(<>) {
>     if(/patternA/) {
>         $_ = <>;
>         if(/patternB/) {
>              #You have detected the pattern accross two lines
>         }
>     }
> }

> I hope this helps,

Well, no, because in your code $_ still contains one line -- the second
one read.  To match /patternB/ across two lines, you must concatenate,
not assign, to $_.

          $_ .= <>;

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 pattern matching across two lines


Quote:



...

Quote:
> > local($/);

> why do you localize $_ when you don't use it?

That is $/, not $_.  The proper localization is in a block, though, so
it doesn't affect the rest of the program.

Quote:
> > $str=<FH>;
> > close(FH);

> > $str =~ s/foo\r\nbar/foobar/gsm;

> Hmmm .. the /s modifier affects the behaviour of the dor '.' inside a
> regexp making it match newlines. And the /m modifier affects the
> behaviour of '^' and '$' making them match within a string.

> You don't have a '.', '^' or '$' in your regexp, so why are you using
> /sm?

Furthermore, the internal representation of newline is the single
character "\n", not the two characters "\r\n", even on systems where
"\r\n" is the external representation.  (Excepting the use of binmode(),
of course, which would be wrong here.)

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 pattern matching across two lines

Quote:




> ...

> > > local($/);

> > why do you localize $_ when you don't use it?

> That is $/, not $_.  The proper localization is in a block, though, so
> it doesn't affect the rest of the program.

Ooops!!! Perhaps I should start wearing glasses!

Thanks for catching that.

--Ala



Wed, 18 Jun 1902 08:00:00 GMT  
 pattern matching across two lines

<URL::">


&& >
&& > > How can I match patterns across two lines?
&& >
&& > Hello, the simplest way I see is:
&& > while(<>) {
&& >     if(/patternA/) {
&& >         $_ = <>;
&& >         if(/patternB/) {
&& >              #You have detected the pattern accross two lines
&& >         }
&& >     }
&& > }
&& >
&& > I hope this helps,
&&
&& Well, no, because in your code $_ still contains one line -- the second
&& one read.  To match /patternB/ across two lines, you must concatenate,
&& not assign, to $_.
&&
&&           $_ .= <>;

Of course, that's going to fail if line 1 matches patternA, and lines
2 and 3 combined match patternB.

Abigail
--
$;                                   # A lone dollar?
=$";                                 # Pod?
$;                                   # The return of the lone dollar?
{Just=>another=>Perl=>Hacker=>}      # Bare block?
=$/;                                 # More pod?
print%;                              # No right operand for %?



Wed, 18 Jun 1902 08:00:00 GMT  
 pattern matching across two lines

Quote:

>Furthermore, the internal representation of newline is the single
>character "\n", not the two characters "\r\n", even on systems where
>"\r\n" is the external representation.

The OP didn't say what platform. He did say the input looked like:

        foo^M
        bar

This says to me:
 * Unix
 * CR is there.

You may want to make it optional:

        s/(foo)\r?\n(bar)/$1$2/g;

Oh, I did some capturing just in case he wants a more generic text
matching than just literal foo's and bar's.

--
        Bart.



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

 Relevant Pages 

1. pattern matching across two lines

2. Pattern matching across multiple lines in a file

3. pattern matching across lines

4. Pattern matching spanning more than one line and substitution between two files -

5. combine two pattern matches to one match?

6. help - grep lines between two line patterns

7. Problem with search and replace across two lines

8. Searching all instances of a pattern across multi-lines

9. Pattern bug matching whitespace in multi-line match?

10. Regex problem: Multiple matches across Multiple lines

11. matching hyphenated words across line ends

12. Greedy matching across multiples lines.

 

 
Powered by phpBB® Forum Software