CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Multiline matching problem: Edit Log



paxxus
New User

Oct 28, 2012, 5:58 AM


Views: 25402
Multiline matching problem

Hello, I'm progressively matching through a string containing many lines and I want to skip lines containing white-space or line-comments; I'm multi-line matching here so white-space includes \n. The following perl program shows how I achieved this:


Code
$s = <<END; 

// Hello

// World
xxx
END

$s =~ m,\G((//.*)?\s*)*,mgc;

print ">", $s, "<\n";
print ">", substr( $s, pos( $s ) ), "<\n";


Here $s is my string and as expected the position is at the 'xxx' after the regexp has eaten through the empty lines and comments. The output of the program is:


Code
> 
// Hello

// World
xxx
<
>xxx
<


If however I change the regexp to:


Code
$s =~ m,\G(\s*|//.*)*,mgc;


Then only the first empty line is eaten and the position is now at the first line-comment, which is not what I want. My problem is that I can't explain why the second version of the regexp doesn't work for me (as you might have guessed this was actually my first failed attempt).

Can anyone help me understand this?


(This post was edited by paxxus on Oct 28, 2012, 6:01 AM)


Edit Log:
Post edited by paxxus (New User) on Oct 28, 2012, 6:01 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives