youtuber

How to add http to the left of the url?

19 posts in this topic

#1 ·  Posted

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

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by aa2zz6
Learning is fun ^^
1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted

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)

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by kylomas
1 person likes this

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

Share this post


Link to post
Share on other sites

#5 ·  Posted

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)

 

1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted

@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://

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (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 by mikell
typo
1 person likes this

Share this post


Link to post
Share on other sites

#8 ·  Posted

Is not Mikel a mistake?
slash marks \\ :blink:

 

Share this post


Link to post
Share on other sites

#9 ·  Posted

youtuber,

The code that I posted does not add the extra line...

kylomas

1 person likes this

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

Share this post


Link to post
Share on other sites

#10 ·  Posted

@kylomas Yes I checked it now no problems thank you :)

 

Share this post


Link to post
Share on other sites

#11 ·  Posted

11 hours ago, youtuber said:

Is not Mikel a mistake?
slash marks \\ :blink:

Yes it is, absolutely
I lazily copied/pasted the code from junkew and completely forgot to change this, sorry  :D
Obviously it should be like this

$newString=stringregexpreplace($testString,"(?m)^(?!\h*http)(?=\S+)", "http://" )

 

1 person likes this

Share this post


Link to post
Share on other sites

#12 ·  Posted

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 

1 person likes this

Share this post


Link to post
Share on other sites

#13 ·  Posted

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 ;)

1 person likes this

Share this post


Link to post
Share on other sites

#14 ·  Posted

@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+)

Share this post


Link to post
Share on other sites

#15 ·  Posted (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 by mikell
1 person likes this

Share this post


Link to post
Share on other sites

#16 ·  Posted

yes i read it in previous post :)
Thank you
I understand better now

Share this post


Link to post
Share on other sites

#18 ·  Posted

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:\\\\" )

 

Share this post


Link to post
Share on other sites

#19 ·  Posted

I guess that's correct :muttley:

$newString=stringregexpreplace($testString,"(?m)^\h*(?!http)(?=\S+)", "http://" )

 

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