apache 2.2 and pcre

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
59iosl30
Forum Newbie
Posts: 5
Joined: Wed Sep 03, 2008 5:23 am

apache 2.2 and pcre

Post by 59iosl30 »

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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: apache 2.2 and pcre

Post by prometheuzz »

59iosl30 wrote:... I need a solution for every directory except folder3Arr

Can anybody help me?

Thank you!
Robert Duffy
Try something like this:

Code: Select all

^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*$
59iosl30
Forum Newbie
Posts: 5
Joined: Wed Sep 03, 2008 5:23 am

Re: apache 2.2 and pcre

Post by 59iosl30 »

prometheuzz wrote:
59iosl30 wrote:... I need a solution for every directory except folder3Arr

Can anybody help me?

Thank you!
Robert Duffy
Try something like this:

Code: Select all

^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*$
Thank you!

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?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: apache 2.2 and pcre

Post by prometheuzz »

59iosl30 wrote:
prometheuzz wrote:
59iosl30 wrote:... I need a solution for every directory except folder3Arr

Can anybody help me?

Thank you!
Robert Duffy
Try something like this:

Code: Select all

^/usr/local/apache2/htdocs/(?:(?!folder3Arr).)*$
Thank you!

This is working:
^/usr/local/apache2/htdocs/(.+/)(?:folder3Arr)
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:But it didn't work with negative look ahead like in your example above.

Do you have another hint?
Could you post a couple of string/paths that should be rejected and a couple of strings/paths that should be accepted?
59iosl30
Forum Newbie
Posts: 5
Joined: Wed Sep 03, 2008 5:23 am

Re: apache 2.2 and pcre

Post by 59iosl30 »

Could you post a couple of string/paths that should be rejected and a couple of strings/paths that should be accepted?
Paths which should be accepted:
test1
test2
test3
test4test
Test55Test
.
.
.
just everything!

and paths which should not be accepted:
folder3Arr
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: apache 2.2 and pcre

Post by prometheuzz »

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?

Code: Select all

/home/me/folder3Arr
/home/me/folder3Arr/other
and what about when a file has the string "folder3Arr" in it:

Code: Select all

/home/me/other/file-name-folder3Arr.txt
should it then also be rejected?
59iosl30
Forum Newbie
Posts: 5
Joined: Wed Sep 03, 2008 5:23 am

Re: apache 2.2 and pcre

Post by 59iosl30 »

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?

Code: Select all

/home/me/folder3Arr
/home/me/folder3Arr/other
and what about when a file has the string "folder3Arr" in it:

Code: Select all

/home/me/other/file-name-folder3Arr.txt
should it then also be rejected?
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?

Code: Select all

/home/me/folder3Arr
/home/me/folder3Arr/other
and what about when a file has the string "folder3Arr" in it:

Code: Select all

/home/me/other/file-name-folder3Arr.txt
should it then also be rejected?
Sorry for my unclearly examples!

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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: apache 2.2 and pcre

Post by prometheuzz »

Okay, thanks. But then my regex was spot on!

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";
  }
}
 
?>
When you run the code, you will get the following output:

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/
which is exactly what you described.
59iosl30
Forum Newbie
Posts: 5
Joined: Wed Sep 03, 2008 5:23 am

Re: apache 2.2 and pcre

Post by 59iosl30 »

prometheuzz wrote:Okay, thanks. But then my regex was spot on!

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";
  }
}
 
?>
When you run the code, you will get the following output:

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/
which is exactly what you described.
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!!!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: apache 2.2 and pcre

Post by prometheuzz »

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!!!
Most regex engines consider '$' to be the end-of-string meta character. Perhaps Apache's regex engine is a bit different on that point.
Anyway, good to hear you've got it sorted! And you're welcome.
Post Reply