Search code examples
regexnotepad++

Using regex for repeating text in Notepad++


I have links like this:

https://d2ynliea65eb6o.cloudfront.net/6100052500-STXMLOPEN/sub_1.m3u8
https://d2ynliea65eb6o.cloudfront.net/6100052499-STXMLOPEN/sub_1.m3u8
https://d2ynliea65eb6o.cloudfront.net/6100052498-STXMLOPEN/sub_1.m3u8

How can I use a regex in Notepad++ to make them like this:

https://d2ynliea65eb6o.cloudfront.net/6100052500-STXMLOPEN/6100052500-STXMLOPENsub_1.m3u8
https://d2ynliea65eb6o.cloudfront.net/6100052499-STXMLOPEN/6100052499-STXMLOPENsub_1.m3u8
https://d2ynliea65eb6o.cloudfront.net/6100052498-STXMLOPEN/6100052498-STXMLOPENsub_1.m3u8

I want to repeat what is between net/ and /sub for each link.


Solution

  • I am assuming you want to repeat the characters before the last /.

    You may try this regex:

    • Regex
    ([^/\n]+)/(?=[^/\n]+$)
    
    • Substitution
    $1/$1
    
    ([^/\n]+)       // any consecutive non-slash and non-linebreak characters, and capture them in group 1
    /               // a slash
    (?=[^/\n]+$)    // lookahead, there must be non-slash and non-linebreak characters followed by the end of a line ahead
    

    Check the proof