2014-05-19 90 views
0

所以我需要用自動替換字符串文本使用stringreplace但我需要隨機輸出。autoit隨機文本替換stringreplace函數

什麼,我需要一個例子是

Stringreplace($string, "and", {also|as well}) 

我的最終目標是隨機基於的「和」字下面的選項以同樣或以及

+1

因此,例如,您希望將「貓和狗以及老鼠」轉變爲「貓還有老鼠以及老鼠」或類似的東西?從編程的角度來看,好的問題在於,英文在輸出中不太可能是正確的。 – Matt

回答

3

我寫了很久以前。

將這個

My name is {John|Peter|Mark}! {Regards|Cheers|Get lost}! 

轉換爲這樣的事情

My name is John! Cheers! 

它與線也打破。

Func SpintaxToTXT($TextWithSpintax) 


    Dim $MSGMSG 
    Dim $lines 

    $lines = StringSplit($TextWithSpintax, @LF) 


    For $z = 1 To $lines[0] 

     If $z > 1 Then $MSGMSG &= @LF 


     $d = StringSplit($lines[$z], "{}") 

     For $i = 1 To $d[0] 

      $MSGSplit = StringSplit($d[$i], "|") 
      If @error Then 
       $MSGMSG &= $MSGSplit[1] 
       ContinueLoop 
      EndIf 
      $MSGMSG &= $MSGSplit[Random(1, $MSGSplit[0], 1)] 

     Next 



    Next 


    Return $MSGMSG 

EndFunc ;==>SpintaxToTXT 
+0

|不能在{}之外使用。修復這個工具_StringBetween – Milos

+0

查看點擊機器人認爲[正確使用範圍](https://stackoverflow.com/review/suggested-edits/18012195)偏離「* ...從原來的意圖的意圖。* 「;請隨意覆蓋。 – user4157124

0

試試這個替換的文字:

Global $options_A = StringSplit('also,as well,bla,blubb,that,this', ',', 2) 

For $i = 0 To 20 
    ConsoleWrite($options_A[Random(0, UBound($options_A) - 1, 1)] & @CRLF) 
Next 
0

這裏有一個我早些時候發表。與StringReplace的工作方式完全相同,不同之處在於不使用替換字符串,而是使用返回替換字符串的函數。使用Xenobiologist的數組方法,你會得到想要的結果。

Local $sTest = "The cat and the dog and the rat." 

ConsoleWrite(_StringReplaceCallback($sTest, "and", _MyCallback) & @LF) 

Func _MyCallback($s) 
    Local Static $aOptions = StringSplit("also|as well", "|") 

    Return $aOptions[Random(1, $aOptions[0], 1)] 
EndFunc ;==>_MyCallback 


Func _StringReplaceCallback($sString, $sFind, $funcReplace, $iOccurence = 0, $iCaseSense = 0) 
    Local $sRet = "" 
    Local $iDir = 1 
    Local $iPos = 1 

    If $iOccurence < 0 Then 
     $iDir = -1 
     $iPos = StringLen($sString) 
    EndIf 

    If $iOccurence = 0 Then $iOccurence = $iDir * StringLen($sString) 

    While 1 
     $i = StringInStr($sString, $sFind, $iCaseSense, $iDir, $iPos) 

     If $iDir > 0 Then 
      If Not $i Or Not $iOccurence Then 
       $sRet &= StringMid($sString, $iPos) 
       ExitLoop 
      EndIf 

      $sRet &= StringMid($sString, $iPos, $i - $iPos) & $funcReplace($sFind) 
      $iPos = $i + StringLen($sFind) 
     Else 
      If Not $i Or Not $iOccurence Then 
       $sRet = StringMid($sString, 1, $iPos) & $sRet 
       ExitLoop 
      EndIf 

      $sRet = $funcReplace($sFind) & StringMid($sString, $i + StringLen($sFind), $iPos - $i - StringLen($sFind) + 1) & $sRet 
      $iPos = $i - 1 
     EndIf 

     If $iOccurence <> 0 Then $iOccurence -= $iDir 
    WEnd 

    Return $sRet 
EndFunc ;==>_StringReplaceCallback 

這也許值得一提的,實際上是有這個簡單的解決方案,提供的替換字符串永遠不要有「和」(否則你就會有一個無限循環),只是替換一個實例在一段時間:

Local $sString = "The cat and the dog and the rat." 
Local $aOptions = StringSplit("also|as well", "|") 

Do 
    $sString = StringReplace($sString, "and", $aOptions[Random(1, $aOptions[0], 1)], 1) 
Until Not @extended 

ConsoleWrite($sString & @LF) 
+0

兩個例子剛剛返回數字84 –

+1

@CesarBielich,什麼?他們都能正常工作,返回'84'是沒有任何意義的。 – Matt

+0

這就是我的想法,但他們做到了。 –