Iraj Posted February 23, 2022 Posted February 23, 2022 Hi Team, Can I trim URL from specific part? Like for example I just need mariaย as user in MsgBox output & not the whole URL. I have used StringTrimLeft, but the issue is my URL keeps on changing from myurl.com to myurl1.com & so on. I am able to achieve using StringTrimLeft, but need more smart way to retrieve only the part after ?user= Thanks! ย #include <MsgBoxConstants.au3> $link = "https://myurl.com/Dashboard.html?user=maria" Local $sUser = StringTrimLeft($link, "39") MsgBox(0, "", $sUser) ย
Solution ad777 Posted February 23, 2022 Solution Posted February 23, 2022 (edited) #include <MsgBoxConstants.au3> $link = "https://myurl.com/Dashboard.html?user=maria" Local $sUser = StringMid($link,StringInStr($link,"?user")+6,-1) MsgBox(0, "", $sUser) ย Edited February 23, 2022 by ad777 none
SOLVE-SMART Posted February 23, 2022 Posted February 23, 2022 (edited) Hi @Iraj, the solution that @ad777 already provided is fine ๐ and the funny thing is that I used basically the same approach few years ago in an old tool ๐ . Here my modified snippet: Global $sUrl = 'https://www.example.com/members.html?user=johndoe' Global $sSearchAfter = '?user=' ConsoleWrite(_GetTextAfterSubString($sUrl, $sSearchAfter)) Func _GetTextAfterSubString($sText, $sSubString) Return StringMid($sText, StringInStr($sText, $sSubString) + StringLen($sSubString)) EndFunc Best regards Sven ________________Stay innovative! Edited February 23, 2022 by SOLVE-SMART ==> AutoIt related: ๐ GitHub, ๐ Discord Server Spoiler ๐ย Au3Forums ๐ฒ AutoIt (en) Cheat Sheet ๐ AutoIt limits/defaults ๐ Code Katas: [...] (comming soon) ๐ญ Collection of GitHub users with AutoIt projects ๐ย False-Positives ๐ฎย Me on GitHub ๐ฌย Opinion about new forum sub category ๐ย UDF wiki list โย VSCode-AutoItSnippets ๐ย WebDriver FAQs ๐จโ๐ซย WebDriver Tutorial (coming soon)
Iraj Posted February 23, 2022 Author Posted February 23, 2022 (edited) 2 hours ago, ad777 said: #include <MsgBoxConstants.au3> $link = "https://myurl.com/Dashboard.html?user=maria" Local $sUser = StringMid($link,StringInStr($link,"?user")+6,-1) MsgBox(0, "", $sUser) ย How can I get only the myurl.com part the similar way ? Keeping in mind that myurl.com will change as myurl1 & so on... Edited February 23, 2022 by Iraj
ad777 Posted February 23, 2022 Posted February 23, 2022 2 hours ago, Iraj said: How can I get only the myurl.com part the similar way ? Keeping in mind that myurl.com will change as myurl1 & so on... $link = "https://myurl.com/Dashboard.html?user=maria" MsgBox(0,"",_GetStringBF($link,"https://","/Dash",5));;5 represent string len of /Dash Func _GetStringBF($stringE,$before,$after,$ns) Local $SText Local $string = StringMid($stringE,StringInStr($stringE,$before)+StringLen($before),-1) For $i = 0 To StringLen($string) if StringMid($string,$i,$ns) = $after Then ExitLoop Else $SText &= StringMid($string,$i,1) EndIf Next Return $SText EndFunc ย none
Nine Posted February 23, 2022 Posted February 23, 2022 (edited) SRE could simplify your script : #include <Constants.au3> #include <Array.au3> $link = "https://myurl.com/Dashboard.html?user=maria" $arr = StringRegExp($link, "\/\/(.*)\/.*\?user=(.*)", 1) _ArrayDisplay($arr) ย Edited February 23, 2022 by Nine โThey did not know it was impossible, so they did itโย โ Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text ย Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker ย Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC ย HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet ย Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector ย Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF ย Multi-Threading Made Easy Interface Object based on Tag ย
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now