Using regex for repeating text in Notepad++

Viewed 80

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.

2 Answers

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

If you want to actually search for and repeat what's in between "net/" and "/sub" and repeat that then you can use:

 (net/(.*?))/sub

replace with:

 $1/$2sub

the second () ie (.*?) will create group $2 which will contain the variable text that occurs between net/ and /sub

the first (), which DOES NOT contain the /sub will contain the text up to, but not including the "/sub" text and put it into $1. If you want to include the "/sub" you would put the ")" on the right side of "/sub".

then $1/$2sub will be the concatenation of $1 with a "/" then $2 then "sub" then the remainder of the text

Related