Selecting all characters after last slash in URL

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Selecting all characters after last slash in URL

Post by Warren » Tue Oct 31, 2017 2:56 pm

I'm trying to resolve a situation where I need to see if current URL matches a stored URL, but only the characters after the last slash. Characters before that may vary, and if so, I still want it to return a match. I've worked out half of the problem in that if compares the end of the current URL to a string.

Code: Select all

SRT>matchEndURL
   Let>targetString=matchEndURL_var_1

   UIGetValue>windowName,{"Address and search bar"},currentURL,
   Length>targetString,strLength
   Position>targetString,currentURL,1,targetStringPos,TRUE
   MidStr>currentURL,targetStringPos,strLength,truncURL

   IF>truncURL=targetString
      Let>matchEndURL_PF=PASS
   ELSE
      Let>matchEndURL_PF=FAIL
   ENDIF

END>matchEndURL


GoSub>matchEndURL,targetString
So far, so good, but it assumes I already have targetString isolated (the string of characters after the last slash in the targetURL.) I don't have targetString yet. What I do have is the targetURL, and still need to find a way to parse targetString from targetURL despite the fact that the number of characters in targetString will vary, and the number of slashes in targetURL will vary.

So, basically, how do I start with targetURL, then extract all the characters after the last slash (targetString) so I can feed targetString into the above SRT?

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Selecting all characters after last slash in URL

Post by Marcus Tettmar » Tue Oct 31, 2017 3:08 pm

Let's hope I've understood you correctly. I think you are saying you want to extract everything after the last slash from a string.

So given this:

https://www.somewhere.com/somedir/somefile.htm

You want to extract this:

somefile.htm

One method would be to split the string based on the / delimiter and take the last part:

Code: Select all

Let>URL=https://www.somewhere.com/somedir/somefile.htm
Separate>URL,/,parts
MessageModal>parts_%parts_count%
Or you could use RegEx:

Code: Select all

Let>URL=https://www.somewhere.com/somedir/somefile.htm
RegEx>([^/]+$),URL,0,matches,nm,0
MesageModal>matches_1
Hope I've understood.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts