Jump to content
youtuber

How to add http to the left of the url?

Recommended Posts

aa2zz6

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 ^^
  • Like 1

Share this post


Link to post
Share on other sites
youtuber

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
kylomas

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
  • Like 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

Share this post


Link to post
Share on other sites
junkew

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)

 

  • Like 1

Share this post


Link to post
Share on other sites
youtuber

@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
mikell

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
  • Like 1

Share this post


Link to post
Share on other sites
youtuber

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

 

Share this post


Link to post
Share on other sites
kylomas

youtuber,

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

kylomas

  • Like 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

Share this post


Link to post
Share on other sites
mikell
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://" )

 

  • Like 1

Share this post


Link to post
Share on other sites
junkew

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 

  • Like 1

Share this post


Link to post
Share on other sites
mikell
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 ;)

  • Like 1

Share this post


Link to post
Share on other sites
youtuber

@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
mikell

\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
  • Like 1

Share this post


Link to post
Share on other sites
youtuber

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

Share this post


Link to post
Share on other sites
mikell
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

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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.