Jump to content
Sign in to follow this  
AutoSam

Find and replace string of a URL

Recommended Posts

AutoSam

Given is any URL starting with http:// and ending with /. I know how to get the URL and put it in memory or as a variable but how do I find certain parts in the URL and replace it with a given string? What functions do I need to use for this please?

Example:

In URL

http://designenvy.aiga.org/solar-bikini-andrew-schneider/
I want to replace
designenvy.aiga.org
with
XYZ.com
and the last
/
of the URL with
.html
.

Thanks for any help towards this, linking me to am example would be perfect, linking me to a good tutorial(s) on this would even be more appreciated. Thanks so much, starting to really enjoy AutoIt partly thanks to the great support for new users in the forum.

Edited by AutoSam

Share this post


Link to post
Share on other sites
Melba23

AutoSam,

This looks as if it will do the trick: ;)

$sURL = "http://designenvy.aiga.org/solar-bikini-andrew-schneider/"

$sNewURL = StringTrimRight(StringRegExpReplace($sURL, "(?U)\/\/.*\/", "\/\/XYZ.com\/"), 1) & ".html"

ConsoleWrite($sNewURL & @CRLF)

SRE explanation:

(?U)          - Look for the smallest match (in case you have multiple /)
\/\/.*\/      - Replace //any-number-of-chars/ - the \ are necessary to escape the /
\/\/XYZ.com\/ - with //XYZ.com/ (again the / is escaped by \)

The StringTrimRight removes the final / and then we concatenate the ".html" :)

All clear? :graduated:

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
AutoSam

Thanks so much, this is freaking awesome. Would you have links to good tutorials about RegEx or perhaps an AutoIt online RegEx tester?

How to learn this stuff with AutoIt? I found this and this but think the later is about RegEx in general and not about RegEx with AutoIt. Any good suggestions?

Again, thanks so much for your fast, correct and very helpful response!

Share this post


Link to post
Share on other sites
Melba23

AutoSam,

I use this site whenever I get out of my depth in the SRE ocean - which is pretty often! ;)

GEOSoft has given us a very good SRE tester. You can get it here - look for the "PCRE Tester for AutoIt" link at bottom left. :graduated:

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
AutoSam
Excellent, thanks so much! :graduated:

Share this post


Link to post
Share on other sites
AutoSam

PCRE Toolkit is getting me quite far however I have been trying all day but somehow here I am stuck. Part of the script I am writing verifies if the user is on the right URL of a site, if not the script repeats until the right URL pattern is matched.

So how can I for example rule out the following locations on the site?

about/
contact/
faq/
popular/
tutorials/
?order=
search/

On the other hand how can I allow locations like these?

URL pattern:

a-z, 0-9, multiple - (dash), one _ (underscore) / then a forward slash, a-z, 0-9, multiple - (dash), multiple _ (underscore)/

This is a string and to understand it better I have given the possibilities in characters, letters and numbers. Note the letters and numbers don't necessarily need to come up on the order written but the URLs do not start with dashes or underscores, if that helps.

The way I though this could be done, but that is probably not such a nice way, is to include the Select...Case...EndSelect function, each case with the above "rule out" locations selected with StringRexExp for example. I find it quite hard to to find the smallest common letter or character from the above "rule out" list. How can I practise that? With PCRE Toolkit or simply somehow get it done by experimenting? Again any help is much appreciated.

Edited by AutoSam

Share this post


Link to post
Share on other sites
Melba23

AutoSam,

to understand it better I have given the possibilities in characters, letters and numbers

No help at all - I have absolutely no idea what the URL is supposed to look like from that! :)

Can you just give me a couple of examples? :graduated:

As to the exclusion problem, you can do this:

Global $aArray[3] = [ _
    "http://designenvy.aiga.org/solar-bikini-andrew-schneider/", _
    "http://designenvy.aiga.org/about/solar-bikini-andrew-schneider/", _
    "http://designenvy.aiga.org/?order/solar-bikini-andrew-schneider/"]

For $i = 0 To 2
    $iRet = StringRegExp($aArray[$i], "(about\/|contact\/|faq\/|popular\/|tutorials\/|\?order\/|search\/)")
    If $iRet Then
        ConsoleWrite("URL " & $aArray[$i] & " is Wrong" & @CRLF)
    Else
        ConsoleWrite("URL " & $aArray[$i] & " is OK" & @CRLF)
    EndIf
Next

Note the use of the "|" to act as an "OR" operator. ;)

M23

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
AutoSam

URL Pattern:

a-z, 0-9, multiple - (dash), one _ (underscore) / then a forward slash, a-z, 0-9, multiple - (dash), multiple _ (underscore)/

Examples:

abz38-gloom-sea/zbca927-ghsj4-fdj549s-8593/
 
daisY/dasiy-goes-to-paris/
 
daisy_bumblebee/dasiy-goes-to-paris48275/
 
Daisy-bumblebee/dasiy-Goes-to-PARis48275/
 
Chess_Masterr_97/master-Looses-his-95728game/
 
9573_hfn462hd/85bfha_jhhws_858BCH_hf84_hfe/

Since basically from looking at the examples I have given above the number of possible matches is much much greater than the number of exclusions I have to make I should not try to from front to back but from back to front. Instead of ruling in all the masses of URLs that fit the pattern I can simply, like you, M23, suggested, filter out the few, but still overseable URLs that I don't want. The OR for the reg exp will come in very handy. I thought I need to find some sort of smallest common ground for all the ones I want ruled out but this OR really came in damn handy.

I am taking the URL from the browser windows with alt-d and ctrl-c, then store that as a variable and then check that variable against the URL pattern I like to have. So your for loop and the stringregexp nicely filter out the URLs I don't want. I am not sure if I will need an array since I am taking the URL variable from the open tab but it surly is a nice way of doing it.

Thanks for your continuous help and support here. This is really nice of you and the rest of the cool crew members here. Last night I dreamt about strings and regex, it was a strange numbers and letters, dream, haha. So yeah, I guess I am actually quite getting into this stuff faster than I ever go into anything else perhaps even.. Again thanks for your contribution to my problems or coding challenges. Thanks!

I can really see how regular expressions are quite the magic, making code slim and fast, avoiding many conditional statements.

Edited by AutoSam

Share this post


Link to post
Share on other sites
Melba23

AutoSam,

Glad I could help. :graduated:

If you want help on any SRE question in future, I suggest putting "SRE" or "RegEx" in the title - that tends to draw the real gurus and not just the hobbyists like me. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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
Sign in to follow this  

×