Search code examples
regexstringregex-lookaroundsregex-groupregex-greedy

RegEx for segmenting a string


I found this Answer for how to paras movie names with RegExp, its working fine but not exactly what i need.

The input string is : 2001.A.Space.Odyssey.1968.720p.BluRay.DD5.1.x264-LiNG.mkv

The Regular Expression that used in the answer like this :^(.+).(\d{4}).+.(mp4|avi|mkv)$ and the output is:

2001.A.Space.Odyssey
1968
mkv 

What i need is like the below :

2001.A.Space.Odyssey
1968
LiNG
mkv 

My try was: ^(.+).(\d{4}).+([^_]).(mp4|avi|mkv)$ but the output is:

2001.A.Space.Odyssey
1968
G
mkv 

How i can get LiNG instead of G ?


Solution

  • This expression has four capturing groups:

    (.+?)\.([0-9]{4}).+?-(.+)\.(mkv|avi|mp4)
    

    where our desired outputs are.

    The name of the movie is in the first capturing group:

    (.+?)
    

    followed by a . and the desired year in the second group:

    ([0-9]{4})
    

    followed by everything else upto the last dash and here we are having the last word:

    (.+)
    

    and the desired extensions are in the last capturing group:

    (mkv|avi|mp4)
    

    DEMO

    Test

    const regex = /(.+?)\.([0-9]{4}).+?-(.+)\.(mkv|avi|mp4)/;
    const str = `2001.A.Space.Odyssey.1968.720p.BluRay.DD5.1.x264-LiNG.mkv`;
    const subst = `$1\n$2\n$3\n$4`;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log('Substitution result: ', result);

    enter image description here