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:
Multiple newlines problem with regexp

 



sergiu1103
New User

Oct 24, 2013, 6:52 AM

Post #1 of 5 (10036 views)
Multiple newlines problem with regexp Can't Post

Hi,

could anybody give me a hint regarding a problem I'm facing with a regexp? I am trying to search for the string: "Options None" that may be found within the following text, inside apache's httpd.conf:

<Directory />
... some text ...
Options None
... other text ...
</Directory>

I am using

$pattern =~ m/\n<Directory \/>.*Options None.*<\/Directory>\n/is

but the search is greedy and is not stopping at the first </Directory>, but rather at the last one in the file.

[^<]\/Directory> also did not do the trick. I think because it's stopping the search before it reaches my text sequence.

Thanks

(This post was edited by sergiu1103 on Oct 24, 2013, 6:53 AM)


BillKSmith
Veteran

Oct 24, 2013, 11:28 AM

Post #2 of 5 (10028 views)
Re: [sergiu1103] Multiple newlines problem with regexp [In reply to] Can't Post

It is hard to tell from your post, but I suspect that your string does not have the initial blank line that your regex requires.

Your regex works fine in the following script.

Code
use strict; 
use warnings;
my $string =
"\n"
."<Directory />\n"
."... some text ...\n"
."Options None\n"
."... other text ...\n"
."</Directory>\n"
;
my $pattern = qr/\n<Directory \/>.*Options None.*<\/Directory>\n/is;

if ($string =~ m/$pattern/) {
print "Pattern found\n";
}
else {
print "Pattern not found\n";
}


My coding of the string may be awkward, but newlines are shown explicitly.
Good Luck,
Bill


sergiu1103
New User

Oct 25, 2013, 3:25 AM

Post #3 of 5 (10019 views)
Re: [BillKSmith] Multiple newlines problem with regexp [In reply to] Can't Post

Thank you for the reply. Unfortunately, it is still not working, and neither is the example you posted.

Try using the following string instead:

my $string =
"\n"
."<Directory />\n"
."... some text ...\n"
."... other text ...\n"
."</Directory>\n"
."Options None\n"
."</Directory>\n"
;

You will see it finds the pattern, although it shouldn't because "Options None" is outside the first encountered <Directory /> ... </Directory>.

This is my problem. I don't want my regexp to bee too greedy.


BillKSmith
Veteran

Oct 25, 2013, 9:05 AM

Post #4 of 5 (10012 views)
Re: [sergiu1103] Multiple newlines problem with regexp [In reply to] Can't Post

"Greediness" has to do with the behavior of the '*' and '+' metacharacters. Because of the greediness of your first '.*', you match the first directory, skip over everything including the second directory and start matching again at option. Using a non-greedy operator would change the behavior, but not fix your problem. You would then start matching at the second directory and everything after that would match.

I do not think it is possible to do what you want with one regex. Why not test for the invalid pattern with one regex. If it is not invalid, test for the valid pattern.
Good Luck,
Bill


sergiu1103
New User

Oct 25, 2013, 11:24 AM

Post #5 of 5 (10007 views)
Re: [BillKSmith] Multiple newlines problem with regexp [In reply to] Can't Post

You're right, I don't think it's possible with one regexp. I created another string with just the <Directory />.*?</Directory>, and searched inside this one instead.

It worked.

Thanks everybody

 
 


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

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