Regex for parsing directory and filename

前端 未结 8 1504
南方客
南方客 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:38

    Try this:

    /^(\/([^/]+\/)*)(.*)$/
    

    It will leave the trailing slash on the path, though.

    0 讨论(0)
  • 2020-11-30 04:39

    What language? and why use regex for this simple task?

    If you must:

    ^(.*)/([^/]*)$
    

    gives you the two parts you wanted. You might need to quote the parentheses:

    ^\(.*\)/\([^/]*\)$
    

    depending on your preferred language syntax.

    But I suggest you just use your language's string search function that finds the last "/" character, and split the string on that index.

    0 讨论(0)
提交回复
热议问题