in reply to regex needed with match operator

Replies are listed 'Best First'.
Re: Re: regex needed with match operator
by hardburn (Abbot) on Apr 28, 2003 at 14:07 UTC
    /\/(\w+)\/foo$/

    Perhaps better as:

    m!/([^/]+)/foo$!

    Advantages:

    • Uses "choose your own quotes" to avoid falling-toothpick syndrome.
    • Some systems allow some funky characters that \w won't match. However, '/' certainly won't be in a filename (unless something really, really weird happens).

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      In fact, the null byte is the only character you can expect not to show up in file names; on Unixoid and DOSish systems, the slash is also guaranteed not to appear, unless maybe you're working with a shot filesystem (I don't think we need to discuss that case). DOSish systems impose a whole lot of additional restrictions of course; none of those exist on Unixoid systems. On other systems than those, all bets are off; I wouldn't be surprised if the slash was a valid part of filenames on something "weird" like VMS.

      Makeshifts last the longest.

        By "slash", I think you meant only forward-slash '/' since '\' is legal for my UFS (OpenBSD) file system and I recall that its fine for Linux (when I last used it eons ago). Both '/' and '\' are illegal for the various Microsoft filesystems. Both '/' and '\' are legal in the Mac file systems except there ':' is reserved. *grin* aint it fun? I just use File::Spec and hope for the best.

        On other systems than those, all bets are off; I wouldn't be surprised if the slash was a valid part of filenames on something "weird" like VMS.

        Nope. VMS is very strict unless things changed in ODS-5 (I haven't worked with V7). ODS-2 only allows upper case alpha, numeric, dollar sign, underscore and hyphen. Slashes in either direction are definitely out. Filenames can have a 39 character name and a 39 character type separated by a period. Older versions of VMS only allowed 9.3 filenames.

        90% of every Perl application is already written.
        dragonchild