skip to Main Content

I have the path ../dir1/dir2/dir3/filename.txt and want to extract just filename between the final and . using regex. The file extension will also not always be .txt.

Currently I am doing (?<=/).+(?=.) but selects from the first , including the directory names. I would love this to just be match-based and not using groups. Oh and using ECMA regex if that’s important.

2

Answers


  1. You can use the below regex to achieve your purpose:

    ([w-]+)..*$
    

    Explanation of the above regex:

    • ([w-]+) – Capturing group that matches one or more (+) word characters (w) or hyphens (-). Usually filenames contains w characters so I used that but if it contains other special characters feel free to amend it as per your need.
    • . – matches a literal dot character.
    • .* – matches any character (.) zero or more times (*).
    • $ – matches end of line. So basically the full regex matches any string that contains a word (consisting of one or more word characters or hyphens) followed by a dot and any characters until the end of the line. The word before the dot is captured for later use and this capturing group gives you your desired filename.

    enter image description here

    const regex = /.*?([w-]+)..*$/gm;
    
    const str = `../dir1/dir2/dir3/filename1.tar.gz
    ../dir1/dir2/dir3/filename2.tar`;
    const subst = `$1`;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log(result);

    REGEX DEMO

    Alternative Way:(Using Javascript functions)

    let paths = `../dir1/dir2/dir3/filename1.tar.gz
    ../dir1/dir2/dir3/filename2.tar`;
    
    paths.split('n').forEach(path => console.log(path.  
    split('/').pop().split('.').shift()));
    
    /* 
    * split('/').pop() - splits the path into an array using the forward slash (/) as the separator
    and removes and returns the last element of the array.  
    
    * split('.').shift() - splits the filename into an array using the dot (.) as the separator, 
    and removes and returns the first element of the array.
    * NOTE: paths.split('n') might not be your exact requirement. I used it to showcase.
    */
    Login or Signup to reply.
  2. If you match the regular expression

    (?:([^/]+).[^./]+|([^./]+))$
    

    either:

    • there is a filename extension, in which case capture group 1 will hold the basename without the extension and capture group 2 will be empty; or
    • the basename has no extension, in which case capture group 2 will hold the basename and capture group 1 will be empty.

    Demo

    We can break this expression down as follows.

    (?:        # begin non-capture group
      (        # begin capture group 1
        [^/]+  # match one or more chars other than (`^`) a forward slash
      )        # end capture group 1
      .       # match a period
      [^./]+   # match one or more chars other than a period or forward slash
    |          # or
      (        # begin capture group 2
        [^./]+ # match one or more chars other than a period or forward slash
      )        # end capture group 2
    )          # end non-capture group
    $          # match the end of the string
    

    You may also wish to hover (the cursor, not your person) over different parts of the expression at the link to obtain explanations of their functions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search