mmoalem

extract a word from URL

9 posts in this topic

hi there all  - after struggling a lot and a lot of help my script is almost working but I need to solve one more issue...

 I need to extract a word out of a URL - the URL has a fixed part (http://www.domain.com/BlaBla/) than a varied part and after another forward slash the page title from which I need a word (so I can identify the browser page that I want to close) - 

so for example:

from the URL http://www.domain.com/mytopfolder/various-possible-words/Silk-By-James I need to extract the word Silk

Have very basic knowledge of coding so please be descriptive if you can

Share this post


Link to post
Share on other sites



3 minutes ago, mmoalem said:

Have very basic knowledge of coding so please be descriptive if you can

Best way to change this have a look in helpfile, specialy StringReplace.

From where you are getting the URL's? If in a Textfile then look for the File* - functions.

Share this post


Link to post
Share on other sites

hi and thanks - had a look at this but not sure how this will work for me (as I said i am a very beginner)...

the script sends the various URLs to a custom Function as $URL. The Function opens the URL in one of different browsers it need to identify/winactivate the browser to perform an action on the page, The window title has only the last string from the URL so I need to use a word from that string for the title in the ControlClick actions

Share this post


Link to post
Share on other sites

Something like this should help you if you have the URL on a variable:

 

#include <Array.au3>
#include <String.au3>

$url = "http://www.domain.com/mytopfolder/various-possible-words/Silk-By-James"

$url_last_parts = StringSplit($url,"/","")

_ArrayDisplay($url_last_parts)

Note in $url_last_parts[6] you will have the last path and then you parse that string to get what you want.

Regards
Alien.

Share this post


Link to post
Share on other sites

Thanks Alien. It kind of takes me half way there but leaves me lost :)

because the window title for the above URL will read "Silk By James" (without the dashs) and for simplicity sake I need to return a variable with the first word after the final forward slash that I can use for the windows title in ControlClick - with you method I am left with an array and not sure how to extract the word i need...

I was hoping for a simple StringReplace solution where I can delete everything before the final forward slash (and all URLs have 3 forward slashes) and than from the first dash onwards... can this be done with stringreplace?

Share this post


Link to post
Share on other sites

This can be done with StringReplace, _StringBetween, StringSplit and more.

You really need to take a look to the help file and make some test by yourselft.

 

#include <Array.au3>
#include <String.au3>

$url = "http://www.domain.com/mytopfolder/various-possible-words/Silk By James"

$url_last_parts = StringSplit($url,"/")

_ArrayDisplay($url_last_parts)

$url_final_word = StringSplit($url_last_parts[6]," ")

_ArrayDisplay($url_final_word)

Regards
Alien.

Share this post


Link to post
Share on other sites

Ok so I think I figured it out

#include <Array.au3>
#include <String.au3>

$url = "http://www.domain.com/mytopfolder/various-possible-words/Silk-By-James"

$url_last_parts = StringSplit($url,"/","")
$FirstWord = StringSplit($url_last_parts[6],"-","")
$SelectedWord = $FirstWord[1]

#include <Array.au3>
#include <String.au3>

$url = "http://www.domain.com/mytopfolder/various-possible-words/Silk-By-James"

$url_last_parts = StringSplit($url,"/","")
$FirstWord = StringSplit($url_last_parts[6],"-","")
$SelectedWord = $FirstWord[1]

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Global $sUrl = 'http://www.domain.com/mytopfolder/various-possible-words/Silk-By-James'
Global $sExtractName = StringTrimLeft($sUrl, StringInStr($sUrl, "/", 0, -1))

ConsoleWrite("Extracted title: " & $sExtractName & @CRLF)
; Either of these only extract the first word
; StringLeft
; ConsoleWrite("Extracted first word: " & StringLeft($sExtractName, StringInStr($sExtractName, "-") -1) & @CRLF)
; StringRegExpReplace. Will work with [:space:] and [-].
ConsoleWrite("Extracted first word: " & StringRegExpReplace($sExtractName, "[\ \-].*", "") & @CRLF)

 

Edited by InunoTaishou
1 person likes this

Share this post


Link to post
Share on other sites

:)

$sUrl = "http://www.domain.com/mytopfolder/various-possible-words/Silk-By-James"

Msgbox(0,"", StringRegExpReplace($sUrl, '.*/([^-]+).*', "$1") )

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now