Removing trailing /, /index.php, and going up a directory

R

richardlev

Hi all,

I'm trying to create a regular expression to go up one directory,
sorta.
Another way of saying would be to remove the trailing part of the path
(with the caveat that /index.php is treated as /).
Perhaps some examples would be better:

/foo/bar/index.php -> /foo/
/foo/bar/ -> /foo/
/foo/bar/somepage.php -> /foo/bar

Basically, i want to treat / and /index.php as the same thing, which
would mean 'remove the trailing directory,' while at the same time
saying 'if i'm looking at a file in this directory, remove the file and
look at this directory.' The idea being, headers on pages go up one
logical layer.

about to make a post -> view forum listing
/forum/post.php -> /forum/

viewing the forum listing -> viewing main website index
/forum/ -> /
/forum/index.php -> /

I hope that makes sense.

Everytime i try to make a regex i always run into the problem of it
treating 'index.php' as 'any other string'
ie: .+((/.+/index.php)|(/.+))$
That last /.+ is causing /foo/bar/index.php to match the trailing
/index.php
All the other approaches i've tried leave me with this same basic
problem and a regex similar to the above.

Any help would be appreciated.

-Richard Levasseur
 
A

A. Sinan Unur

(e-mail address removed) wrote in @v46g2000cwv.googlegroups.com:
Hi all,

I'm trying to create a regular expression to go up one directory,
sorta.
Another way of saying would be to remove the trailing part of the path
(with the caveat that /index.php is treated as /).
Perhaps some examples would be better:

/foo/bar/index.php -> /foo/
/foo/bar/ -> /foo/
/foo/bar/somepage.php -> /foo/bar

You can do it in steps:

#!/usr/bin/perl

use strict;
use warnings;

while ( my $p = <DATA> ) {
chomp $p;
last unless length $p;
$p =~ s{index\.php\z}{};
$p =~ s{ (?:\w+/\z) | (?:/\w+\.php) }{}x;
print "$p\n";
}

__DATA__
/foo/bar/index.php
/foo/bar/
/foo/bar/somepage.php

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top