sigur7
sigur7

Reputation: 936

Regex in Visual Studio Code Search/Replace to change urls with any structure

I am looking to replace all old CDN URLs with my new site URL. The CDN URLs vary in length and structure and sometimes the CDN prefix can be slightly different too.

I have been using the following, but these also changes some other CDN URLs such as FontAwesome and others that I don't want. So just need something a bit more specific

(https?://)(.*?)(/.*)

URLS TO CAPTURE

https://lirp-cdn.multiscreensite.com/624dfs85te/dms3rep/multi/opt/logo-400w.jpg
https://lirp-cdn.multiscreensite.com/624dfs85te/dms3rep/multi/opt/home04-96006935-1920w.jpg
https://irp-cdn.multiscreensite.com/624dfs85te/dms3rep/multi/home02.jpg

DESIRED RESULT

https://mywebsite.com/logo-400w.jpg
https://mywebsite.com/home04-96006935-1920w.jpg
https://mywebsite.com/home02.jpg

Upvotes: 1

Views: 1831

Answers (2)

Ryszard Czech
Ryszard Czech

Reputation: 18631

Use

(https?://)l?irp-cdn\.multiscreensite\.com(?:/.*)?(/.*)

Replace with:

$1mywebsite.com$2

See proof

EXPLANATION:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    http                     'http'
--------------------------------------------------------------------------------
    s?                       's' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    ://                      '://'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  l?                       'l' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  irp-cdn                  'irp-cdn'
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  multiscreensite          'multiscreensite'
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  com                      'com'
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    /                        '/'
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    /                        '/'
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \2

Upvotes: 0

rioV8
rioV8

Reputation: 28763

Incorporate the domain of the CDN

You must remove the ? from the *? because you want all the directories in group 2, only the last / in group 3.

(https?://)(l?irp-cdn.multiscreensite.com.*)(/.*)

Upvotes: 1

Related Questions