Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I have been using this regular expression to parse *nix paths as part of a module.

my $rxParsePath = qr {(?x) # Use extended regular expression syntax to # allow comments and white space ^ # Anchor pattern to beginning of string (?=.) # Zero-width look ahead assertion to ensure # that there must be at least one character # for the match to succeed (.*/)? # A memory grouping (1st) for path, greedy # match of any characters up to and including # the rightmost slash (the path part) with a # quantifier of '?' (0 or 1), i.e. there # may or may not be a directory part ( # Open memory grouping (2nd) for file name (.*?) # A memory grouping (3rd) for file name stub # of a non-greedy match of any character # without a quantifier since, if there is a # file name part, at least some of it will # form a stub otherwise it would be a dot-file ( # A memory grouping (4th) for file name # extension (?<=[^/]) # zero width look behind assertion such # that following pattern will only succeed # if preceded by any caracter other than # a slash '/' \.[^.]+ # a literal dot '.' followed by one or more # non-dots )? # Close memory grouping (4th) with a quantifier # of '?' (0 or 1), i.e. there may or may not # be a file name extension part )? # Close memory grouping (2nd) with a quantifier # of '?' (0 or 1), i.e. there may or may not # be a file name part $ # Anchor pattern to end of string };

Here is a short script using it, without extended syntax for brevity, to pull out the elements of a *nix path.

use strict; use warnings; my $rxParsePath = qr{^(?=.)(.*/)?((.*?)((?<=[^/])\.[^.]+)?)?$}; print <<EOT; Path Directory File Name File Stub Extension EOT print map { sprintf qq{%20s%15s%15s%10s%10s\n}, @$_ } map { [ $_, map { defined $_ ? $_ : q{} } m{$rxParsePath} ] } qw { /etc/motd /var/adm/messages.1 .alias a.html ab.cd.txt /bin };

The results

Path Directory File Name File Stub Extension /etc/motd /etc/ motd motd /var/adm/messages.1 /var/adm/ messages.1 messages .1 .alias .alias .alias a.html a.html a .html ab.cd.txt ab.cd.txt ab.cd .txt /bin / bin bin

I hope this will be useful.

Cheers,

JohnGG

Update: Corrected superfluous text from C&P error.


In reply to Re: Regex To Remove File Extension by johngg
in thread Regex To Remove File Extension by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (7)
    As of 2014-08-20 13:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (113 votes), past polls