search /\/.*[^.].*$/

K

Ken Sington

Not what I had expected.
I was hoping
/\/.*[^.].*$/
would find a "/" and does not contain any number of dots between any
number of characters.
A shash followed by any number of word characters is valid.
A shash followed by any number of word characters then a trailling slash
is valid.
A shash followed by any number of word characters then a dot then some
more chars is not valid.

If it has a dot between the final shash and end of string, it's not valid.

Here I expected a slash, anychar, must not have '.', anychar, $anchor
my $str = "/tmp/helloworld";
$str =~ /\/.*[^.].*$/
unless perl sees it as slash, anychar all the way to the end, so ignores
the rest of the regex.

other examples:
$str = "/tmp/hello"; <--- yes
$str = "/var/http-docs/cgi-bin/"; <--- yes
$str = "/var/http-docs/cgi-bin"; <--- yes
$str = "/var/http-docs/cgi-bin/hello.pl"; <--- no
$str = "/var/http-docs/cgi-bin/.blah"; <--- no
 
B

Bob Walton

Ken said:
Not what I had expected.
I was hoping
/\/.*[^.].*$/
would find a "/" and does not contain any number of dots between any
number of characters.


Well, your hopes aside, it will find a / followed by the largest number
of any character possible, followed by one non-period character,
followed by the largest number of any character possible, followed by
the end of the string. So for example, the string:

/blah/blah/blah.blah

will match -- / will match the first /, .* will match
blah/blah/blah.bla, [^.] will match h, and .* will match the empty
string. It seems like that's not what you want. Maybe something like:

$str=~/\/[^.]*$/

would do what you want (it does your examples correctly, anyway).

But you should probably make life easy for yourself, and

use File::Basename;

or similar, because the above regexp will probably not do what you want on

something like:

/blah.dir/blah/blah

for which the match will succeed, but starting with the second /
character. Is that what you want? Maybe:

$str=~/^\/[^.]*$/

? You don't define the exact behavior you want.


....
 
K

Ken Sington

would find a "/" and does not contain any number of dots between any
number of characters.
A shash followed by any number of word characters is valid.
A shash followed by any number of word characters then a trailling slash
is valid.
A shash followed by any number of word characters then a dot then some
more chars is not valid.

Ah, I see, thanks for pointing that out.
I ment those rules for what comes after the last slash.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top