apache 2.2 and pcre
Moderator: General Moderators
apache 2.2 and pcre
Hello!
I'm new in pcre and tried to solve the following issue:
I tried with pcre to match everything except
folder3Arr
of a list of different alphaonly directories (except the above one has one digit in it) with the pcre:
<DirectoryMatch "^/usr/local/apache2/htdocs/(.+/)?[a-zA-Z]*">
...
</DirectoryMatch>
But the directory folder3Arr is always matching.
I tried a lot of different things but didn't find a solution how I can match for a digit (like "3") or a substring (like "3Ar") in the middle of a string.
I also tried it with look ahead and look behind like
<DirectoryMatch "^/usr/local/apache2/htdocs/folder(?!3Arr)"></DirectoryMatch>
<DirectoryMatch "^/usr/local/apache2/htdocs/(?<!folder)3Arr"></DirectoryMatch>
<DirectoryMatch "^/usr/local/apache2/htdocs/(.+/)(?!3Arr)"></DirectoryMatch>
But it seams that look ahead and look behind don't work with (.+/) -> I need a solution for every directory except folder3Arr
Can anybody help me?
Thank you!
Robert Duffy
I'm new in pcre and tried to solve the following issue:
I tried with pcre to match everything except
folder3Arr
of a list of different alphaonly directories (except the above one has one digit in it) with the pcre:
<DirectoryMatch "^/usr/local/apache2/htdocs/(.+/)?[a-zA-Z]*">
...
</DirectoryMatch>
But the directory folder3Arr is always matching.
I tried a lot of different things but didn't find a solution how I can match for a digit (like "3") or a substring (like "3Ar") in the middle of a string.
I also tried it with look ahead and look behind like
<DirectoryMatch "^/usr/local/apache2/htdocs/folder(?!3Arr)"></DirectoryMatch>
<DirectoryMatch "^/usr/local/apache2/htdocs/(?<!folder)3Arr"></DirectoryMatch>
<DirectoryMatch "^/usr/local/apache2/htdocs/(.+/)(?!3Arr)"></DirectoryMatch>
But it seams that look ahead and look behind don't work with (.+/) -> I need a solution for every directory except folder3Arr
Can anybody help me?
Thank you!
Robert Duffy
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: apache 2.2 and pcre
Try something like this:59iosl30 wrote:... I need a solution for every directory except folder3Arr
Can anybody help me?
Thank you!
Robert Duffy
Code: Select all
^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*$Re: apache 2.2 and pcre
Thank you!prometheuzz wrote:Try something like this:59iosl30 wrote:... I need a solution for every directory except folder3Arr
Can anybody help me?
Thank you!
Robert Duffy
Code: Select all
^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*$
This is working:
^/usr/local/apache2/htdocs/(.+/)(?:folder3Arr)
But it didn't work with negative look ahead like in your example above.
Do you have another hint?
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: apache 2.2 and pcre
I really doubt it. Perhaps with some example input you fed it, but not all. (?:...) just means that anything in between the parenthesis is not "remembered" by the regex engine (aka a "non-capturing group").59iosl30 wrote:Thank you!prometheuzz wrote:Try something like this:59iosl30 wrote:... I need a solution for every directory except folder3Arr
Can anybody help me?
Thank you!
Robert Duffy
Code: Select all
^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*$
This is working:
^/usr/local/apache2/htdocs/(.+/)(?:folder3Arr)
Could you post a couple of string/paths that should be rejected and a couple of strings/paths that should be accepted?59iosl30 wrote:But it didn't work with negative look ahead like in your example above.
Do you have another hint?
Re: apache 2.2 and pcre
Paths which should be accepted:Could you post a couple of string/paths that should be rejected and a couple of strings/paths that should be accepted?
test1
test2
test3
test4test
Test55Test
.
.
.
just everything!
and paths which should not be accepted:
folder3Arr
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: apache 2.2 and pcre
That isn't really specific enough I'm afraid. Do you want paths containing folder3Arr to be rejected or paths with the top-most directory being folder3Arr to be rejected? That's why I asked for a number of invalid input directories. You now posted just one, and I can't extract this information from it.
I.E., are both not allowed?
and what about when a file has the string "folder3Arr" in it:
should it then also be rejected?
I.E., are both not allowed?
Code: Select all
/home/me/folder3Arr
/home/me/folder3Arr/otherCode: Select all
/home/me/other/file-name-folder3Arr.txtRe: apache 2.2 and pcre
prometheuzz wrote:That isn't really specific enough I'm afraid. Do you want paths containing folder3Arr to be rejected or paths with the top-most directory being folder3Arr to be rejected? That's why I asked for a number of invalid input directories. You now posted just one, and I can't extract this information from it.
I.E., are both not allowed?
and what about when a file has the string "folder3Arr" in it:Code: Select all
/home/me/folder3Arr /home/me/folder3Arr/other
should it then also be rejected?Code: Select all
/home/me/other/file-name-folder3Arr.txt
Sorry for my unclearly examples!prometheuzz wrote:That isn't really specific enough I'm afraid. Do you want paths containing folder3Arr to be rejected or paths with the top-most directory being folder3Arr to be rejected? That's why I asked for a number of invalid input directories. You now posted just one, and I can't extract this information from it.
I.E., are both not allowed?
and what about when a file has the string "folder3Arr" in it:Code: Select all
/home/me/folder3Arr /home/me/folder3Arr/other
should it then also be rejected?Code: Select all
/home/me/other/file-name-folder3Arr.txt
It's just necessary for /usr/local/apache2/htdocs/*/ like on your first example. Files doesn't matter.
Paths which should be accepted:
/usr/local/apache2/htdocs/test1/
/usr/local/apache2/htdocs/test2/
/usr/local/apache2/htdocs/test3/
/usr/local/apache2/htdocs/test4test/
/usr/local/apache2/htdocs/Test55Test/
.
.
.
just everything (and all its subdirectories)!
and paths which should not be accepted:
/usr/local/apache2/htdocs/folder3Arr/
folder3Arr doesn't occur in the next levels of subdirectories.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: apache 2.2 and pcre
Okay, thanks. But then my regex was spot on!
Here's how I tested it:
When you run the code, you will get the following output:
which is exactly what you described.
Here's how I tested it:
Code: Select all
<?php
$tests = array(
'/usr/local/apache2/htdocs/test1/',
'/usr/local/apache2/htdocs/test2/',
'/usr/local/apache2/htdocs/test3/',
'/usr/local/apache2/htdocs/test4test/',
'/usr/local/apache2/htdocs/Test55Test/',
'/usr/local/apache2/htdocs/folder3Arr/',
'/usr/local/apache2/htdocs/test/folder3Arr/',
'/usr/local/apache2/htdocs/folder3Arr/test/'
);
$regex = '#^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*$#';
foreach($tests as $t) {
if(preg_match($regex, $t)) {
echo " Accpeted: $t\n";
} else {
echo "! Rejected: $t\n";
}
}
?>Code: Select all
Accpeted: /usr/local/apache2/htdocs/test1/
Accpeted: /usr/local/apache2/htdocs/test2/
Accpeted: /usr/local/apache2/htdocs/test3/
Accpeted: /usr/local/apache2/htdocs/test4test/
Accpeted: /usr/local/apache2/htdocs/Test55Test/
! Rejected: /usr/local/apache2/htdocs/folder3Arr/
! Rejected: /usr/local/apache2/htdocs/test/folder3Arr/
! Rejected: /usr/local/apache2/htdocs/folder3Arr/test/Re: apache 2.2 and pcre
Now it works with the following:prometheuzz wrote:Okay, thanks. But then my regex was spot on!
Here's how I tested it:
When you run the code, you will get the following output:Code: Select all
<?php $tests = array( '/usr/local/apache2/htdocs/test1/', '/usr/local/apache2/htdocs/test2/', '/usr/local/apache2/htdocs/test3/', '/usr/local/apache2/htdocs/test4test/', '/usr/local/apache2/htdocs/Test55Test/', '/usr/local/apache2/htdocs/folder3Arr/', '/usr/local/apache2/htdocs/test/folder3Arr/', '/usr/local/apache2/htdocs/folder3Arr/test/' ); $regex = '#^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*$#'; foreach($tests as $t) { if(preg_match($regex, $t)) { echo " Accpeted: $t\n"; } else { echo "! Rejected: $t\n"; } } ?>
which is exactly what you described.Code: Select all
Accpeted: /usr/local/apache2/htdocs/test1/ Accpeted: /usr/local/apache2/htdocs/test2/ Accpeted: /usr/local/apache2/htdocs/test3/ Accpeted: /usr/local/apache2/htdocs/test4test/ Accpeted: /usr/local/apache2/htdocs/Test55Test/ ! Rejected: /usr/local/apache2/htdocs/folder3Arr/ ! Rejected: /usr/local/apache2/htdocs/test/folder3Arr/ ! Rejected: /usr/local/apache2/htdocs/folder3Arr/test/
<DirectoryMatch "^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*"></DirectoryMatch>
For some reason I had to remove the end anchor "$" under the DirectoryMatch-Tag. I don't know why.
Thank you very much for your help!!!
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: apache 2.2 and pcre
Most regex engines consider '$' to be the end-of-string meta character. Perhaps Apache's regex engine is a bit different on that point.59iosl30 wrote:...
Now it works with the following:
<DirectoryMatch "^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*"></DirectoryMatch>
For some reason I had to remove the end anchor "$" under the DirectoryMatch-Tag. I don't know why.
Thank you very much for your help!!!
Anyway, good to hear you've got it sorted! And you're welcome.