Jump to content

Recommended Posts

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

 

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

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

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)

 

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

 

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

 

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 

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

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

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
  • Recently Browsing   0 members

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