Regex for parsing directory and filename

前端 未结 8 1556
南方客
南方客 2020-11-30 03:55

I\'m trying to write a regex that will parse out the directory and filename of a fully qualified path using matching groups.

so...

/         


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 04:37

    In languages that support regular expressions with non-capturing groups:

    ((?:[^/]*/)*)(.*)
    

    I'll explain the gnarly regex by exploding it...

    (
      (?:
        [^/]*
        /
      )
      *
    )
    (.*)
    

    What the parts mean:

    (  -- capture group 1 starts
      (?:  -- non-capturing group starts
        [^/]*  -- greedily match as many non-directory separators as possible
        /  -- match a single directory-separator character
      )  -- non-capturing group ends
      *  -- repeat the non-capturing group zero-or-more times
    )  -- capture group 1 ends
    (.*)  -- capture all remaining characters in group 2
    

    Example

    To test the regular expression, I used the following Perl script...

    #!/usr/bin/perl -w
    
    use strict;
    use warnings;
    
    sub test {
      my $str = shift;
      my $testname = shift;
    
      $str =~ m#((?:[^/]*/)*)(.*)#;
    
      print "$str -- $testname\n";
      print "  1: $1\n";
      print "  2: $2\n\n";
    }
    
    test('/var/log/xyz/10032008.log', 'absolute path');
    test('var/log/xyz/10032008.log', 'relative path');
    test('10032008.log', 'filename-only');
    test('/10032008.log', 'file directly under root');
    

    The output of the script...

    /var/log/xyz/10032008.log -- absolute path
      1: /var/log/xyz/
      2: 10032008.log
    
    var/log/xyz/10032008.log -- relative path
      1: var/log/xyz/
      2: 10032008.log
    
    10032008.log -- filename-only
      1:
      2: 10032008.log
    
    /10032008.log -- file directly under root
      1: /
      2: 10032008.log
    

提交回复
热议问题