Jump to content

How to add http to the left of the url?


Recommended Posts

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 by aa2zz6
Learning is fun ^^
Link to comment
Share on other sites

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

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 by kylomas

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

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)

 

Link to comment
Share on other sites

@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

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 by mikell
typo
Link to comment
Share on other sites

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 

Link to comment
Share on other sites

\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 by mikell
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...