LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 03-28-2008, 08:07 AM   #1
adymcc
Member
 
Registered: Dec 2006
Posts: 31

Rep: Reputation: 17
Perl Regex multiline match issues


Hi,

I have a file which contains multiple lines which I want to be able to search as one string, but I am having problems getting the regular expression match to continue searching following a newline character.

The contents of the file I'm testing with is:

This is my
multi-line string

For completeness here is the file printing the control characters:

Code:
user@host~]$ cat -vet file
This is my$
multi-line string$
Here's the regex that seems correct:

Code:
#!/usr/bin/perl

open (FILE, "<", file) or die "can't open file";

if (<FILE> =~ /.*(This.*)/s) { print "$1";}
else {print "failed";}

close FILE

Here's the output:

Code:
user@host~]$ ./perl.pl | cat -vet
This is my$
Surely with the /s modifier it should continue past the newline character and display the whole file?
 
Old 03-28-2008, 10:08 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
The problem is not the match or the regex, I think, but how you are opening the file. The context for if is scalar, so it just takes just the first, single line of the file and it tests that against your regex. Try it this way instead:
Code:
perl -e 'print "$1\n" if "This is my\nmulti-line string" =~ /.*(This.*)/s'
The match operator tests against a scalar - ie, it takes one item and tests that against your regex - for example, if you open the file in a while loop, then perl treats each line, one at a time, as $_ and tests $_ against the regex. To test the whole file at once you would have to first slurp the whole file into a scalar.
Code:
#!/usr/bin/perl
use strict;
use warnings;

open(my $file, "<", "file")
    or die "Can't open file: $!";
my $text = do { local $/; <$file> };

if ($text =~ /.*(This.*)/s) {
    print $1;
}
Edit: see this from Perldoc's FAQ http://perldoc.perl.org/5.8.8/perlfa...all-at-once%3f

Last edited by Telemachos; 03-28-2008 at 10:57 AM. Reason: Added link to FAQ
 
Old 03-31-2008, 09:45 AM   #3
adymcc
Member
 
Registered: Dec 2006
Posts: 31

Original Poster
Rep: Reputation: 17
Thanks for this Telemachos, thats nailed it. And all the time I was looking at the regex match!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to look for the shortest match using regex, bascially the opposite of .* new_2_unix Linux - Newbie 8 01-08-2008 09:21 AM
Perl Multiline Variables and inline PHP vargadanis Programming 13 10-18-2007 02:04 AM
perl + regex issues. kurrupt Programming 2 10-11-2006 09:33 AM
help me match this regex line (easy) JustinHoMi Programming 7 03-17-2002 01:43 AM
Perl print Multiline help! CragStar Programming 1 02-06-2002 08:48 AM


All times are GMT -5. The time now is 07:51 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration