Author |
Message |
Niti #1 / 9
|
 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 |
|
 |
Federico Abasca #2 / 9
|
 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 |
|
 |
Rich Mor #3 / 9
|
 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 |
|
 |
Ala Qumsie #4 / 9
|
 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 |
|
 |
Larry Rosle #5 / 9
|
 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 |
|
 |
Larry Rosle #6 / 9
|
 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 |
|
 |
Ala Qumsie #7 / 9
|
 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 |
|
 |
Abiga #8 / 9
|
 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 |
|
 |
Bart Lateu #9 / 9
|
 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 |
|
|