youtuber Posted October 7, 2017 Share Posted October 7, 2017 Hi, I want to add http to the left of Url. if there is not http $myurl = "autoitscript.com" & @CRLF & _ "test.com/http://test.com" & @CRLF & _ "example.com" If Not StringInStr($myurl, "http") And "https" Then $aAddHttp = "http://" & $myurl ConsoleWrite($aAddHttp & @CRLF) EndIf Link to comment Share on other sites More sharing options...
aa2zz6 Posted October 7, 2017 Share Posted October 7, 2017 (edited) The URL's are listed as an array. Ignore the StringReplace if you didn't want http changed to https. #include <Array.au3> Local $iMax = 10 Local $arr[$iMax] = [3, "autoitscript.com", "test.com/", "http://test.com"] For $i = 1 To $arr[0] $a = $arr[$i] $b = StringReplace($a, "http://", "") If Not StringInStr($b, "https") Then $aAddHttp = "https://" & $b ConsoleWrite($aAddHttp & @LF) EndIf Next Edited October 7, 2017 by aa2zz6 Learning is fun ^^ youtuber 1 Link to comment Share on other sites More sharing options...
youtuber Posted October 8, 2017 Author Share Posted October 8, 2017 Your example is not what I want I want something like this Global $ScriptDir = @ScriptDir If StringRight($ScriptDir, 1) <> "\" Then $ScriptDir &= "\" Global $File_to_written = $ScriptDir & "replace.txt" Local $FileOpen = FileOpen($File_to_written, 2) Local $ReadString = FileRead($FileOpen) ;$ReadString = StringReplace($ReadString,@CRLF,@LF) For $i = 1 To UBound($ReadString)-1 $a = $ReadString[$i] $b = StringLeft($a,4) If Not StringInStr($b, "http") Then $aAddHttp = "http://" & $a $ReplaceString = StringReplace($aAddHttp,@CRLF,@LF) $FileOpen = FileOpen($File_to_written, 2) FileWrite($FileOpen, $ReplaceString) EndIf Next FileClose($FileOpen) Link to comment Share on other sites More sharing options...
kylomas Posted October 8, 2017 Share Posted October 8, 2017 (edited) youtuber, Roughly following your logic... #include <array.au3> #include <file.au3> ; open the output file Global $File_to_written = @ScriptDir & "\replace.txt" Local $FileOpen = FileOpen($File_to_written, 2) If $FileOpen = -1 Then Exit MsgBox(17, 'ERROR', 'File open failed for output file') ; read the input file Local $ReadString = FileRead(@ScriptDir & '\dummy_urls.txt') Local $aURLS = StringSplit($ReadString, @CRLF, 3) For $1 = 0 To UBound($aURLS) - 1 If StringLeft($aURLS[$1], 3) = 'www' Then $aURLS[$1] = 'http://' & $aURLS[$1] Next _FileWriteFromArray($FileOpen, $aURLS) FileClose($FileOpen) ShellExecute($File_to_written) file used for testing... dummy_urls.txt kylomas Edited October 8, 2017 by kylomas youtuber 1 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
junkew Posted October 8, 2017 Share Posted October 8, 2017 this will come close. You have to tweak the urls that starts with h but as i do not know your data (maybe all start with www) $testSTring="www.google.com" & @crlf $testSTring=$testSTring & "http:\\google.com" & @crlf $testSTring=$testSTring & "https:\\www.google.com" & @crlf $testSTring=$testSTring & "www.github.com" & @crlf $testSTring=$testSTring & "telegraaf.nl" & @crlf $testSTring=$testSTring & "hardwareinfo.nl" & @crlf $testSTring=$testSTring & "www.github.com" & @crlf consolewrite($testSTring) $testSTring=@crlf&$testString consolewrite("===========" & @CRLF) $newString=stringregexpreplace($testString,"\r\n([^h])",@CRLF & "http:\\\1" ) $newstring=stringmid($newstring,3) consolewrite($newString) youtuber 1 FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
youtuber Posted October 8, 2017 Author Share Posted October 8, 2017 @kylomas Thanks! this is what i want But the Problem last line adds one extra http: // #include <array.au3> #include <file.au3> Global $File_to_written = @ScriptDir & "\replace.txt" Local $FileOpen = FileOpen($File_to_written, 2) If $FileOpen = -1 Then Exit MsgBox(17, 'ERROR', 'File open failed for output file') Local $ReadString = FileRead(@ScriptDir & '\dummy_urls.txt') Local $aURLS = StringSplit($ReadString, @CRLF, 3) For $1 = 0 To UBound($aURLS) - 1 $b = StringLeft($aURLS[$1],4) If Not StringInStr($b, "http") Then $aURLS[$1] = 'http://' & $aURLS[$1] Next _FileWriteFromArray($FileOpen, $aURLS) FileClose($FileOpen) ShellExecute($File_to_written) dummy_urls.txt http://www.autoitscript.com www.google.com msdn.com http://www.ibm.net replace.txt http://www.autoitscript.com http://www.google.com http://msdn.com http://www.ibm.net http:// Link to comment Share on other sites More sharing options...
mikell Posted October 8, 2017 Share Posted October 8, 2017 (edited) Maybe this ? $testSTring="www.google.com" & @crlf $testSTring&="http:\\google.com" & @crlf $testSTring&="https:\\www.google.com" & @crlf $testSTring&=" " & @crlf $testSTring&="www.github.com" & @crlf $testSTring&="telegraaf.nl" & @crlf $testSTring&="hardwareinfo.nl" & @crlf $testSTring&="www.github.com" & @crlf $testSTring&=@crlf consolewrite($testSTring) consolewrite("===========" & @CRLF) $newString=stringregexpreplace($testString,"(?m)^(?!\h*http)(?=\S+)", "http:\\\\" ) consolewrite($newString) Edited October 8, 2017 by mikell typo youtuber 1 Link to comment Share on other sites More sharing options...
youtuber Posted October 8, 2017 Author Share Posted October 8, 2017 Is not Mikel a mistake? slash marks \\ Link to comment Share on other sites More sharing options...
kylomas Posted October 9, 2017 Share Posted October 9, 2017 youtuber, The code that I posted does not add the extra line... kylomas youtuber 1 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
youtuber Posted October 9, 2017 Author Share Posted October 9, 2017 @kylomas Yes I checked it now no problems thank you Link to comment Share on other sites More sharing options...
mikell Posted October 9, 2017 Share Posted October 9, 2017 11 hours ago, youtuber said: Is not Mikel a mistake? slash marks \\ Yes it is, absolutely I lazily copied/pasted the code from junkew and completely forgot to change this, sorry Obviously it should be like this $newString=stringregexpreplace($testString,"(?m)^(?!\h*http)(?=\S+)", "http://" ) youtuber 1 Link to comment Share on other sites More sharing options...
junkew Posted October 9, 2017 Share Posted October 9, 2017 and the explanation ;-) Look at the beginning of a line and lookahead if there is NOT http then do replacement of the matched beginning of line character. (?m) for "multi-line mode" makes the caret and dollar match at the start and end of each line in the subject string. In Ruby, (?m) makes the dot match all characters, without affecting the caret and dollar which always match at the start and end of each line in Ruby. In Tcl, (?m) also prevents the dot from matching line breaks. ^ is beginning of line Lookahead assertions ?= for positive assertions and ?! for negative assertions \h any horizontal whitespace marker * = 0 or more http is just the characters to check and the () around is group1 lookahead regex very powerfull and leads to many shorthands and no need to do for loop syntax you can check on these pages http://www.pcre.org/original/doc/html/pcrepattern.html youtuber 1 FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
mikell Posted October 9, 2017 Share Posted October 9, 2017 2 hours ago, junkew said: Look at the beginning of a line and lookahead if there is NOT http May I add, look also - using (?=\S+) - if start of line is followed by at least one non-space char This allows to skip empty or blank lines youtuber 1 Link to comment Share on other sites More sharing options...
youtuber Posted October 9, 2017 Author Share Posted October 9, 2017 @mikell Mikell It works fine too (?=\N+) $newString=stringregexpreplace($testString,"(?m)^(?!\h*http)(?=\N+)", "http://" ) Which is better? Which should I use? Is this (?=\N+) Or is this (?=\S+) Link to comment Share on other sites More sharing options...
mikell Posted October 9, 2017 Share Posted October 9, 2017 (edited) \N means : no newline character (but allows horizontal white spaces), while \S means : no whitespace at all (whatever) This is easy to check by just doing the replacement in my code in post #7 Edit To fit your needs obviously \S is better. You probably read my previous post before I edited it Edited October 9, 2017 by mikell youtuber 1 Link to comment Share on other sites More sharing options...
youtuber Posted October 9, 2017 Author Share Posted October 9, 2017 yes i read it in previous post Thank you I understand better now Link to comment Share on other sites More sharing options...
junkew Posted October 9, 2017 Share Posted October 9, 2017 but this one will fail (even when you iterate array so not specifically regex related) so its important to understand how all hyperlinks in your validation file are looking $testSTring&=" www.telegraaf.nl" & @crlf FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
mikell Posted October 9, 2017 Share Posted October 9, 2017 2 hours ago, junkew said: but this one will fail Right Just put the leading \h* out of the group $newString=stringregexpreplace($testString,"(?m)^\h*(?!http)(?=\S+)", "http:\\\\" ) Link to comment Share on other sites More sharing options...
youtuber Posted October 9, 2017 Author Share Posted October 9, 2017 I guess that's correct $newString=stringregexpreplace($testString,"(?m)^\h*(?!http)(?=\S+)", "http://" ) Link to comment Share on other sites More sharing options...
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