Slee
Slee

Reputation: 28248

capture value to the left of querystring using regsub

Here is a sample URL:

https://mydomainname.net/productimages/1679/T716AP1_lg.jpg?w=125&h=125&tstamp=05/19/2016%2015:08:30

What I want from this is just:

/productimages/1679/T716AP1_lg

My current code is:

regsub(req.url, "^/(.*)\.(.*)$", "\1")

Which works great until there are multiple query string parameters link in my example above, seems the & is causing me problems.

Upvotes: 1

Views: 244

Answers (2)

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

^https:\/\/[^\/]+\/([^.]*)\.jpg

Regular expression visualization

This expression will do the following:

  • find the subpage and filename from the given link, assuming the filename is a jpg

Example

Live Demo

https://regex101.com/r/nZ7eX7/1

Sample text

https://mydomainname.net/productimages/1679/T716AP1_lg.jpg?w=125&h=125&tstamp=05/19/2016%2015:08:30

Sample Matches

productimages/1679/T716AP1_lg

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  https:                   'https:'
----------------------------------------------------------------------
  \/                       '/'
----------------------------------------------------------------------
  \/                       '/'
----------------------------------------------------------------------
  [^\/]+                   any character except: '\/' (1 or more
                           times (matching the most amount possible))
----------------------------------------------------------------------
  \/                       '/'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^.]*                    any character except: '.' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \.jpg                      '.jpg'
----------------------------------------------------------------------

Upvotes: 1

Bohemian
Bohemian

Reputation: 425033

Try capturing non-dots/questions instead:

regsub(req.url, "^http://.*?/([^?.]+).*$", "\1")

Upvotes: 2

Related Questions