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

Discussion in 'Perl Misc' started by Ken Sington, Jun 20, 2004.

  1. Ken Sington

    Ken Sington Guest

    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
     
    Ken Sington, Jun 20, 2004
    #1
    1. Advertising

  2. Ken Sington

    Bob Walton Guest

    Ken Sington wrote:

    > 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.


    ....


    > 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


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Jun 20, 2004
    #2
    1. Advertising

  3. Ken Sington

    Ken Sington Guest

    > 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.
     
    Ken Sington, Jun 20, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TGFrc2htaSBOYXJheWFuYW4uUg==?=

    Google search result like site search!! How?

    =?Utf-8?B?TGFrc2htaSBOYXJheWFuYW4uUg==?=, May 5, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    699
    Lucas Tam
    May 6, 2005
  2. Andy
    Replies:
    1
    Views:
    367
    Jack Klein
    Nov 25, 2003
  3. Anand Pillai

    String search vs regexp search

    Anand Pillai, Oct 12, 2003, in forum: Python
    Replies:
    10
    Views:
    607
    Anand Pillai
    Oct 15, 2003
  4. mason66
    Replies:
    0
    Views:
    430
    mason66
    Jul 27, 2006
  5. Abby Lee
    Replies:
    5
    Views:
    443
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page