Jump to content

String functions and one-liners.


Malkey
 Share

Recommended Posts

I have noticed that a lot of array functions can be replaced with an equivalent string function.

This is a collection of String functions and one-liners for String manipulation, which, I hope, will encourage thinking outside of the array box.

;
If FileExists("test.txt") = 0 Then
    Local $sText, $y
    For $x = 0 To 35
        $y &= Chr(Mod($x, 10) + 49)
        $sText &= StringFormat("%02.0f", $x) & " :0 based   Base 1: " & StringFormat("%02.0f ) ", $x + 1) & StringReplace($y, ":", "0") & @CRLF
    Next
    ConsoleWrite($sText & @CRLF)
    $file = FileOpen("test.txt", 2)
    FileWrite($file, $sText)
    FileClose($file)
EndIf


Local $sFile = "test.txt"
; =============== Strings from file =============================================
Local $iLineNum = 11

$sREResult = StringRegExpReplace(FileRead($sFile), "^(?:.*\v+?|$){" & $iLineNum & "}(.*\v+|$)(?s).*", "\1") ; Return line no. 0-based
MsgBox(0, "Return line no. " & $iLineNum & " (0-based)", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "^(.*\v+?|$){" & $iLineNum & "}(?s).*", "\1") ; Return line no. 1-based
MsgBox(0, "Return line no. " & $iLineNum & " (1-based)", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?ms)(?:.*)^(.*\v+^.*)$", "\1") ; Return string last 2 lines
MsgBox(0, "Return string last 2 lines: ", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)(?s)(?:.*)^(.*)$", "\1") ; Return string last line
MsgBox(0, "Return string last line", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)^(.*\v+)(?s).*", "\1") ; Return string 1st line
MsgBox(0, "Return string 1st line", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)^(.*\v+.*\v+)(?s).*", "\1") ; Return string 1st and second line
MsgBox(0, "Return string 1st and second line", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)((^.*\v+|\v+){" & $iLineNum & "})(?s)(.*)", "\1") ; Return string up to line no. 0-based.
MsgBox(0, "Return string up to line no. " & $iLineNum & " (0-based)", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)(?:(?:^.*\v+|\v+){" & $iLineNum & "})(?:^.*\v+)(?s)(.*)", "\1") ; Return string all above  line no. 0-based. line
MsgBox(0, "Return string all above  line no. " & $iLineNum & " (0-based)", $sREResult)
; ===============> End of Strings from file =============================================

;==== StringReplaceRE =================
Local $sReplaceLine = "=== REPLACEMENT LINE ==="
Local $iLineNum = 12 ; 0-base line number - Replace this line.
$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)((^.*\v+|\v+){" & $iLineNum & "})(.*\v+|\v+)(?s)(.*)", "\1" & $sReplaceLine & @CRLF & "\4") ; Return string with replaced line. (0-based. line) line
MsgBox(0, "Return string with replaced line no. " & $iLineNum & " (0-based)", $sREResult)


;========= StringInsertRE==================
Local $sInsertLine = "=== INSERTED LINE ==="
Local $iLine = 33 ; 0-base line number - Replace this line.(If $iLine >= Total number of line Then will insert at second last line position.
$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)((^.*\v+|\v+){" & $iLine & "})(?s)(.*)", "\1" & $sInsertLine & @CRLF & "\3") ; Return string with inserted line. (0-based. line)
MsgBox(0, "Return string with inserted line no. " & $iLine & " (0-based)", $sREResult)


;#cs
Local $sStr = "C:\a\path\temp-.au3"
Local $sInserted = "-" & @LF
Local $iNum = 4 ; Will insert after 4th character
Local $sREResult = StringRegExpReplace($sStr, "(.{" & $iNum & "})(.*)$", "${1}" & $sInserted & "\2")
MsgBox(0, 'Will insert "-" & @LF after character number ' & $iNum, $sREResult)
;====> End of StringInsertRE==================
;#ce

;====== StringInBetween ========================
Local $sStr = "C:1a2path3tempqwau3", $vCase = '(?i)', $sStart = "a2", $sEnd = "3t"
Local $sREResult = StringRegExpReplace($sStr, '(?s)(?:.*)' & $vCase & $sStart & '(.*?)' & $sEnd & "(.*)", "\1")
MsgBox(0, 'StringInBetween ', "From: " & $sStr & @CRLF & 'return string in between "' & $sStart & '" and "' & $sEnd & '"' & @CRLF & " Result: " & $sREResult)

Local $sStr = "C:1a2Path3tempqwau3", $vCase = '(?i)', $sStart = 5, $sEnd = 9
Local $sREResult = StringRegExpReplace($sStr, '(?s)' & $vCase & "(?:.{" & $sStart & '})(.{' & $sEnd - $sStart & "})(.*)", "\1")
MsgBox(0, 'StringInBetween ', "From: " & $sStr & @CRLF & 'return string in between character number "' & $sStart & '" and character "' & $sEnd & '"' & @CRLF & " Result: " & $sREResult)
;======> End of StringInBetween ==================

; ====  StringSplitRE =========================================
Local $sStr = "one##two##three##four####six##seven##eight", $sSplitOn = "##"
Local $sREResult = StringRegExpReplace($sStr, "(.*?)(" & $sSplitOn & ")", "\1" & @CRLF); a string
MsgBox(0, 'StringSplitRE (Not an array) ', "From: " & $sStr & @CRLF & 'split on  "' & $sSplitOn & '"' & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)
; ====> End of StringSplitRE ==================================


$sString = "123" & @CRLF & "abc" & @CRLF & "273" & @CRLF & "123456" & @CRLF & "19271" & @CRLF & "axycz" & @CRLF
Local $sSelect = "12"
$sREResult = StringRegExpReplace($sString, "(?i)(?m)((" & $sSelect & ")(.*)(\v+|$|\z))|^(?:.*(\v+|$|\z))", "\2\3"); select
MsgBox(0, 'Select from multi-lines. ', "Select from: " & @CRLF & $sString & @CRLF & 'Starting with: "' & $sSelect & '"' & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)

Local $sSelect = ".*7"
$sREResult = StringRegExpReplace($sString, "(?i)(?m)((" & $sSelect & ")(.*)(\v+|$|\z))|^(?:.*(\v+|$|\z))", "\2\3"); select
MsgBox(0, 'Select from multi-lines. ', "Select from: " & @CRLF & $sString & @CRLF & 'Lines with: "' & $sSelect & '"' & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)

$sREResult = StringRegExpReplace($sString, "(?i)(?m)((.*)(\v+|$|\z)){2}|^(?:.*(\v+|$|\z))", "\2\3"); Select every 2nd row
MsgBox(0, 'Select from multi-lines. ', "Select from: " & @CRLF & $sString & @CRLF & 'Every 2nd lines ' & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)


;#cs
; ================= _StringRepeatRE ==========================
;ConsoleWrite(_StringRepeatRE("1234567890", 3) & @CRLF)
MsgBox(0, '_StringRepeatRE', _StringRepeatRE("=", 30) & @CRLF & _StringRepeatRE("1234567890", 3) & @CRLF & _StringRepeatRE("=", 30) & @CRLF)

Func _StringRepeatRE($sString, $iRepeatCount)
    Return StringRegExpReplace(StringFormat("%" & $iRepeatCount & "s", " "), ".", $sString)
EndFunc   ;==>_StringRepeatRE
; ==============> End of _StringRepeatRE ====================
;#ce

;#cs
;StringToHexRE
$sREResulta = Execute(StringTrimRight(StringRegExpReplace("I like AutoIt3", "(.)", 'hex(asc("\1"),2) & '), 2))
MsgBox(0, 'StringToHexRE', "From: " & "I like AutoIt3" & @CRLF & @CRLF & " Result: " & @CRLF & $sREResulta)

;HexToStringRE
$sREResult = Execute(StringTrimRight(StringRegExpReplace($sREResulta, "(..)", 'chr(dec("\1")) & '), 2))
MsgBox(0, 'HexToStringRE', "From: " & $sREResulta & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)
;#ce



;
Local $sStr = "12" & @CRLF & "34" & @CRLF & "56" & @CRLF & "7" & @CRLF & "89" & @CRLF & "10" & _
        @CRLF & "11" & @CRLF & "13" & @CRLF & "14" & @CRLF & "15" & @CRLF & "16" & @CRLF & "99" & @CRLF
;#cs
;=====  pop push ===========

;_StringPopRightRE
Local $sREResult = StringRegExpReplace($sStr, "(?:.*\v+)*(.+?)\v+$", "\1"); Retrieve right most entry or bottom line contexts.
MsgBox(0, '_StringPopRightRE', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Retrieve right most entry or bottom line contexts." & @CRLF & " Result: " & @CRLF & $sREResult)

;_StringPopLeftRE
$sREResult = StringRegExpReplace($sStr, "(.*?)(?:\v+)(?s)(?:.*)$", "\1") ; Retrieve left most entry or top line contexts.
MsgBox(0, '_StringPopLeftRE', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Retrieve left most entry or top line contexts." & @CRLF & " Result: " & @CRLF & $sREResult)

Local $sStr1 = StringRegExpReplace($sStr, "(?s)(.*\v+)*(.+\v+)\Z$", "\1") ; Delete  right
MsgBox(0, '_Delete  right', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Delete  right" & @CRLF & " Result: " & @CRLF & $sStr1)

$sStr = StringRegExpReplace($sStr1, "(?:.*)*\v+(?s)(.+?)$", "\1") ; Delete  left
MsgBox(0, 'Delete  left', "From: " & @CRLF & $sStr1 & @CRLF & @CRLF & "Delete  left" & @CRLF & " Result: " & @CRLF & $sStr)
; ===================> End of pop push================================
;#ce

;#cs
; ====================== Display 2D array style ===========================
Local $sStr = "12" & @CRLF & "34" & @CRLF & "56" & @CRLF & "7" & @CRLF & "89" & @CRLF & "10" & _
        @CRLF & "11" & @CRLF & "13" & @CRLF & "14" & @CRLF & "15" & @CRLF & "16" & @CRLF & "99" & @CRLF

$iNumOfCols = 2

MsgBox(0, 'Display 2D array style (Not an array)', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Dispay " & $iNumOfCols & " columns." & @CRLF & " Result: " & @CRLF & _
        StringRegExpReplace(StringRegExpReplace($sStr, "(.*[^\v])(?:\v+)", "${1}" & @TAB), _
        "((.*?" & @TAB & "){" & $iNumOfCols & "})", "${1}" & @CRLF)) ; Display 2D array style)

MsgBox(0, 'Display 2D array style (Not an array)', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Dispay 4 columns." & @CRLF & " Result: " & @CRLF & _StringDisplayRE($sStr, 4) & @CRLF)


Func _StringDisplayRE($sStr, $iNumOfCol = 0)
    Return StringRegExpReplace(StringRegExpReplace($sStr, "(.*[^\v])(?:\v+)", "${1}" & @TAB), _
            "((.*?" & @TAB & "){" & $iNumOfCol & "})", "${1}" & @CRLF)
EndFunc   ;==>_StringDisplayRE
; =======================> End of  Display 2D array style =================
;#ce
;

;
;#cs
; ========================  _StringSortRE ================================
Local $sStr = "12" & @CRLF & "34" & @CRLF & "56" & @CRLF & "7" & @CRLF & "89" & @CRLF & "10" & _
        @CRLF & "11" & @CRLF & "13" & @CRLF & "14" & @CRLF & "15" & @CRLF & "16" & @CRLF & "99" & @CRLF

MsgBox(0, 'StringSortRE ', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr) Result: " & @CRLF & _StringSortRE($sStr))
MsgBox(0, 'StringSortRE ', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr, 0, 1) Result: " & @CRLF & _StringSortRE($sStr, 0, 1))
MsgBox(0, 'StringSortRE ', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr, 1, 1) Result: " & @CRLF & _StringSortRE($sStr, 1, 1))
MsgBox(0, 'StringSortRE ', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr, 1, 0) Result: " & @CRLF & _StringSortRE($sStr, 1, 0))


;For descending order set $iDescending to a numeric value not zero.
;To sort numbers set $iNumbers to a numeric value not zero.
Func _StringSortRE($sStr, $iDescending = 0, $iNumbers = 0)
    Local $iCount, $bPass = False, $x = 1
    StringReplace($sStr, @CRLF, @CRLF)
    $iCount = @extended
    Local $sStr1 = $sStr

    While Not $bPass
        $bPass = True
        For $x = 0 To $iCount - 2
            $sline1 = StringRegExpReplace($sStr1, "^(?:.*\v+?|$){" & $x & "}(.*?)(?:\v+|$)(?s).*", "\1")
            $sline2 = StringRegExpReplace($sStr1, "^(?:.*\v+?|$){" & $x + 1 & "}(.*?)(?:\v+|$)(?s).*", "\1")
            If ($iNumbers <> 0 And $iDescending = 0 And Number($sline1) > Number($sline2)) Or _
                    ($iNumbers = 0 And $iDescending = 0 And $sline1 > $sline2) Or _
                    ($iNumbers <> 0 And $iDescending <> 0 And Number($sline1) < Number($sline2)) Or _
                    ($iNumbers = 0 And $iDescending <> 0 And $sline1 < $sline2) Then
                $sStr1 = StringRegExpReplace($sStr1, "(?m)((^.*\v+|\v+){" & $x & "})(.*\v+|\v+)(?s)(.*)", "${1}" & $sline2 & @CRLF & "\4") ; Return string with replaced line. (0-based. line) line
                $sStr1 = StringRegExpReplace($sStr1, "(?m)((^.*\v+|\v+){" & ($x + 1) & "})(.*\v+|\v+)(?s)(.*)", "${1}" & $sline1 & @CRLF & "\4") ; Return string with replaced line. (0-based. line) line
                $bPass = False
            EndIf
        Next
    WEnd
    Return $sStr1
EndFunc   ;==>_StringSortRE
;
; ===================> End of _StringSortRE ================================
;#ce
;

I like _StringRepeatRE at about line #100 in script, a lot.

Here is a link to similar one-liners (no arrays) for extracting parts from a fullpath, file name string.

Added:Post#5 - String displayed as a transposed 2D array.

Edited by Malkey
Link to comment
Share on other sites

Cheers Malkey; you're exactly right when you say this type of nfo encourages outside of the box thinking :D - if you or anyone else could be bothered to do benchmarks on these functions it would be a brilliant and natural followup on the work you have done. Could it theory mean that a performance based replacement UDF include file for all these operations.

Thanks again,

Sunaj

Link to comment
Share on other sites

I have noticed that a lot of array functions can be replaced with an equivalent string function.

This is a collection of String functions and one-liners for String manipulation, which, I hope, will encourage thinking outside of the array box.

;
If FileExists("test.txt") = 0 Then
    Local $sText, $y
    For $x = 0 To 35
        $y &= Chr(Mod($x, 10) + 49)
        $sText &= StringFormat("%02.0f", $x) & " :0 based   Base 1: " & StringFormat("%02.0f ) ", $x + 1) & StringReplace($y, ":", "0") & @CRLF
    Next
    ConsoleWrite($sText & @CRLF)
    $file = FileOpen("test.txt", 2)
    FileWrite($file, $sText)
    FileClose($file)
EndIf


Local $sFile = "test.txt"
; =============== Strings from file =============================================
Local $iLineNum = 11

$sREResult = StringRegExpReplace(FileRead($sFile), "^(?:.*\v+?|$){" & $iLineNum & "}(.*\v+|$)(?s).*", "\1") ; Return line no. 0-based
MsgBox(0, "Return line no. " & $iLineNum & " (0-based)", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "^(.*\v+?|$){" & $iLineNum & "}(?s).*", "\1") ; Return line no. 1-based
MsgBox(0, "Return line no. " & $iLineNum & " (1-based)", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?ms)(?:.*)^(.*\v+^.*)$", "\1") ; Return string last 2 lines
MsgBox(0, "Return string last 2 lines: ", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)(?s)(?:.*)^(.*)$", "\1") ; Return string last line
MsgBox(0, "Return string last line", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)^(.*\v+)(?s).*", "\1") ; Return string 1st line
MsgBox(0, "Return string 1st line", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)^(.*\v+.*\v+)(?s).*", "\1") ; Return string 1st and second line
MsgBox(0, "Return string 1st and second line", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)((^.*\v+|\v+){" & $iLineNum & "})(?s)(.*)", "\1") ; Return string up to line no. 0-based.
MsgBox(0, "Return string up to line no. " & $iLineNum & " (0-based)", $sREResult)

$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)(?:(?:^.*\v+|\v+){" & $iLineNum & "})(?:^.*\v+)(?s)(.*)", "\1") ; Return string all above  line no. 0-based. line
MsgBox(0, "Return string all above  line no. " & $iLineNum & " (0-based)", $sREResult)
; ===============> End of Strings from file =============================================

;==== StringReplaceRE =================
Local $sReplaceLine = "=== REPLACEMENT LINE ==="
Local $iLineNum = 12 ; 0-base line number - Replace this line.
$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)((^.*\v+|\v+){" & $iLineNum & "})(.*\v+|\v+)(?s)(.*)", "\1" & $sReplaceLine & @CRLF & "\4") ; Return string with replaced line. (0-based. line) line
MsgBox(0, "Return string with replaced line no. " & $iLineNum & " (0-based)", $sREResult)


;========= StringInsertRE==================
Local $sInsertLine = "=== INSERTED LINE ==="
Local $iLine = 33 ; 0-base line number - Replace this line.(If $iLine >= Total number of line Then will insert at second last line position.
$sREResult = StringRegExpReplace(FileRead($sFile), "(?m)((^.*\v+|\v+){" & $iLine & "})(?s)(.*)", "\1" & $sInsertLine & @CRLF & "\3") ; Return string with inserted line. (0-based. line)
MsgBox(0, "Return string with inserted line no. " & $iLine & " (0-based)", $sREResult)


;#cs
Local $sStr = "C:\a\path\temp-.au3"
Local $sInserted = "-" & @LF
Local $iNum = 4 ; Will insert after 4th character
Local $sREResult = StringRegExpReplace($sStr, "(.{" & $iNum & "})(.*)$", "${1}" & $sInserted & "\2")
MsgBox(0, 'Will insert "-" & @LF after character number ' & $iNum, $sREResult)
;====> End of StringInsertRE==================
;#ce

;====== StringInBetween ========================
Local $sStr = "C:1a2path3tempqwau3", $vCase = '(?i)', $sStart = "a2", $sEnd = "3t"
Local $sREResult = StringRegExpReplace($sStr, '(?s)(?:.*)' & $vCase & $sStart & '(.*?)' & $sEnd & "(.*)", "\1")
MsgBox(0, 'StringInBetween ', "From: " & $sStr & @CRLF & 'return string in between "' & $sStart & '" and "' & $sEnd & '"' & @CRLF & " Result: " & $sREResult)

Local $sStr = "C:1a2Path3tempqwau3", $vCase = '(?i)', $sStart = 5, $sEnd = 9
Local $sREResult = StringRegExpReplace($sStr, '(?s)' & $vCase & "(?:.{" & $sStart & '})(.{' & $sEnd - $sStart & "})(.*)", "\1")
MsgBox(0, 'StringInBetween ', "From: " & $sStr & @CRLF & 'return string in between character number "' & $sStart & '" and character "' & $sEnd & '"' & @CRLF & " Result: " & $sREResult)
;======> End of StringInBetween ==================

; ====  StringSplitRE =========================================
Local $sStr = "one##two##three##four####six##seven##eight", $sSplitOn = "##"
Local $sREResult = StringRegExpReplace($sStr, "(.*?)(" & $sSplitOn & ")", "\1" & @CRLF); a string
MsgBox(0, 'StringSplitRE (Not an array) ', "From: " & $sStr & @CRLF & 'split on  "' & $sSplitOn & '"' & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)
; ====> End of StringSplitRE ==================================


$sString = "123" & @CRLF & "abc" & @CRLF & "273" & @CRLF & "123456" & @CRLF & "19271" & @CRLF & "axycz" & @CRLF
Local $sSelect = "12"
$sREResult = StringRegExpReplace($sString, "(?i)(?m)((" & $sSelect & ")(.*)(\v+|$|\z))|^(?:.*(\v+|$|\z))", "\2\3"); select
MsgBox(0, 'Select from multi-lines. ', "Select from: " & @CRLF & $sString & @CRLF & 'Starting with: "' & $sSelect & '"' & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)

Local $sSelect = ".*7"
$sREResult = StringRegExpReplace($sString, "(?i)(?m)((" & $sSelect & ")(.*)(\v+|$|\z))|^(?:.*(\v+|$|\z))", "\2\3"); select
MsgBox(0, 'Select from multi-lines. ', "Select from: " & @CRLF & $sString & @CRLF & 'Lines with: "' & $sSelect & '"' & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)

$sREResult = StringRegExpReplace($sString, "(?i)(?m)((.*)(\v+|$|\z)){2}|^(?:.*(\v+|$|\z))", "\2\3"); Select every 2nd row
MsgBox(0, 'Select from multi-lines. ', "Select from: " & @CRLF & $sString & @CRLF & 'Every 2nd lines ' & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)


;#cs
; ================= _StringRepeatRE ==========================
;ConsoleWrite(_StringRepeatRE("1234567890", 3) & @CRLF)
MsgBox(0, '_StringRepeatRE', _StringRepeatRE("=", 30) & @CRLF & _StringRepeatRE("1234567890", 3) & @CRLF & _StringRepeatRE("=", 30) & @CRLF)

Func _StringRepeatRE($sString, $iRepeatCount)
    Return StringRegExpReplace(StringFormat("%" & $iRepeatCount & "s", " "), ".", $sString)
EndFunc   ;==>_StringRepeatRE
; ==============> End of _StringRepeatRE ====================
;#ce

;#cs
;StringToHexRE
$sREResulta = Execute(StringTrimRight(StringRegExpReplace("I like AutoIt3", "(.)", 'hex(asc("\1"),2) & '), 2))
MsgBox(0, 'StringToHexRE', "From: " & "I like AutoIt3" & @CRLF & @CRLF & " Result: " & @CRLF & $sREResulta)

;HexToStringRE
$sREResult = Execute(StringTrimRight(StringRegExpReplace($sREResulta, "(..)", 'chr(dec("\1")) & '), 2))
MsgBox(0, 'HexToStringRE', "From: " & $sREResulta & @CRLF & @CRLF & " Result: " & @CRLF & $sREResult)
;#ce



;
Local $sStr = "12" & @CRLF & "34" & @CRLF & "56" & @CRLF & "7" & @CRLF & "89" & @CRLF & "10" & _
        @CRLF & "11" & @CRLF & "13" & @CRLF & "14" & @CRLF & "15" & @CRLF & "16" & @CRLF & "99" & @CRLF
;#cs
;=====  pop push ===========

;_StringPopRightRE
Local $sREResult = StringRegExpReplace($sStr, "(?:.*\v+)*(.+?)\v+$", "\1"); Retrieve right most entry or bottom line contexts.
MsgBox(0, '_StringPopRightRE', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Retrieve right most entry or bottom line contexts." & @CRLF & " Result: " & @CRLF & $sREResult)

;_StringPopLeftRE
$sREResult = StringRegExpReplace($sStr, "(.*?)(?:\v+)(?s)(?:.*)$", "\1") ; Retrieve left most entry or top line contexts.
MsgBox(0, '_StringPopLeftRE', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Retrieve left most entry or top line contexts." & @CRLF & " Result: " & @CRLF & $sREResult)

Local $sStr1 = StringRegExpReplace($sStr, "(?s)(.*\v+)*(.+\v+)\Z$", "\1") ; Delete  right
MsgBox(0, '_Delete  right', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Delete  right" & @CRLF & " Result: " & @CRLF & $sStr1)

$sStr = StringRegExpReplace($sStr1, "(?:.*)*\v+(?s)(.+?)$", "\1") ; Delete  left
MsgBox(0, 'Delete  left', "From: " & @CRLF & $sStr1 & @CRLF & @CRLF & "Delete  left" & @CRLF & " Result: " & @CRLF & $sStr)
; ===================> End of pop push================================
;#ce

;#cs
; ====================== Display 2D array style ===========================
Local $sStr = "12" & @CRLF & "34" & @CRLF & "56" & @CRLF & "7" & @CRLF & "89" & @CRLF & "10" & _
        @CRLF & "11" & @CRLF & "13" & @CRLF & "14" & @CRLF & "15" & @CRLF & "16" & @CRLF & "99" & @CRLF

$iNumOfCols = 2

MsgBox(0, 'Display 2D array style (Not an array)', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Dispay " & $iNumOfCols & " columns." & @CRLF & " Result: " & @CRLF & _
        StringRegExpReplace(StringRegExpReplace($sStr, "(.*[^\v])(?:\v+)", "${1}" & @TAB), _
        "((.*?" & @TAB & "){" & $iNumOfCols & "})", "${1}" & @CRLF)) ; Display 2D array style)

MsgBox(0, 'Display 2D array style (Not an array)', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "Dispay 4 columns." & @CRLF & " Result: " & @CRLF & _StringDisplayRE($sStr, 4) & @CRLF)


Func _StringDisplayRE($sStr, $iNumOfCol = 0)
    Return StringRegExpReplace(StringRegExpReplace($sStr, "(.*[^\v])(?:\v+)", "${1}" & @TAB), _
            "((.*?" & @TAB & "){" & $iNumOfCol & "})", "${1}" & @CRLF)
EndFunc   ;==>_StringDisplayRE
; =======================> End of  Display 2D array style =================
;#ce
;

;
;#cs
; ========================  _StringSortRE ================================
Local $sStr = "12" & @CRLF & "34" & @CRLF & "56" & @CRLF & "7" & @CRLF & "89" & @CRLF & "10" & _
        @CRLF & "11" & @CRLF & "13" & @CRLF & "14" & @CRLF & "15" & @CRLF & "16" & @CRLF & "99" & @CRLF

MsgBox(0, 'StringSortRE ', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr) Result: " & @CRLF & _StringSortRE($sStr))
MsgBox(0, 'StringSortRE ', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr, 0, 1) Result: " & @CRLF & _StringSortRE($sStr, 0, 1))
MsgBox(0, 'StringSortRE ', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr, 1, 1) Result: " & @CRLF & _StringSortRE($sStr, 1, 1))
MsgBox(0, 'StringSortRE ', "From: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr, 1, 0) Result: " & @CRLF & _StringSortRE($sStr, 1, 0))


;For descending order set $iDescending to a numeric value not zero.
;To sort numbers set $iNumbers to a numeric value not zero.
Func _StringSortRE($sStr, $iDescending = 0, $iNumbers = 0)
    Local $iCount, $bPass = False, $x = 1
    StringReplace($sStr, @CRLF, @CRLF)
    $iCount = @extended
    Local $sStr1 = $sStr

    While Not $bPass
        $bPass = True
        For $x = 0 To $iCount - 2
            $sline1 = StringRegExpReplace($sStr1, "^(?:.*\v+?|$){" & $x & "}(.*?)(?:\v+|$)(?s).*", "\1")
            $sline2 = StringRegExpReplace($sStr1, "^(?:.*\v+?|$){" & $x + 1 & "}(.*?)(?:\v+|$)(?s).*", "\1")
            If ($iNumbers <> 0 And $iDescending = 0 And Number($sline1) > Number($sline2)) Or _
                    ($iNumbers = 0 And $iDescending = 0 And $sline1 > $sline2) Or _
                    ($iNumbers <> 0 And $iDescending <> 0 And Number($sline1) < Number($sline2)) Or _
                    ($iNumbers = 0 And $iDescending <> 0 And $sline1 < $sline2) Then
                $sStr1 = StringRegExpReplace($sStr1, "(?m)((^.*\v+|\v+){" & $x & "})(.*\v+|\v+)(?s)(.*)", "${1}" & $sline2 & @CRLF & "\4") ; Return string with replaced line. (0-based. line) line
                $sStr1 = StringRegExpReplace($sStr1, "(?m)((^.*\v+|\v+){" & ($x + 1) & "})(.*\v+|\v+)(?s)(.*)", "${1}" & $sline1 & @CRLF & "\4") ; Return string with replaced line. (0-based. line) line
                $bPass = False
            EndIf
        Next
    WEnd
    Return $sStr1
EndFunc   ;==>_StringSortRE
;
; ===================> End of _StringSortRE ================================
;#ce
;

I like _StringRepeatRE at about line #100 in script, a lot.

Here is a link to similar one-liners (no arrays) for extracting parts from a fullpath, file name string.

Brilliant!!!

I don't get to imagine like "DEV" didn't have this idea...

Fantastic, fast, elegant and simple, still reduces the size of the executable.

Will we transform this in UDF?

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

This script displays a "@CRLF" separated, segmented string as a transposed 2D array.

;
; ====================== Display 2D array style ===========================
Local $sStr = "12" & @CRLF & "34" & @CRLF & "56" & @CRLF & "7" & @CRLF & "89" & @CRLF & "10" & _
        @CRLF & "11" & @CRLF & "13" & @CRLF & "14" & @CRLF & "15" & @CRLF & "16" & @CRLF & "99" & @CRLF


MsgBox(0, 'Display 2D array style (Not an array)', "From: " & @CRLF & $sStr & @CRLF & @CRLF & _
        "Dispay 2 columns." & @CRLF & " Result: " & @CRLF & _StringDisplayRE($sStr, 2) & @CRLF & @CRLF & _
        "Dispay 2 columns transposed." & @CRLF & " Result: " & @CRLF & _StringDisplay2DTransposedRE($sStr, 2) & @CRLF)

MsgBox(0, 'Display 2D array style (Not an array)', "From: " & @CRLF & $sStr & @CRLF & @CRLF & _
        "Dispay 4 columns." & @CRLF & " Result: " & @CRLF & _StringDisplayRE($sStr, 4) & @CRLF & @CRLF & _
        "Dispay 4 columns transposed." & @CRLF & " Result: " & @CRLF & _StringDisplay2DTransposedRE($sStr, 4) & @CRLF)



Func _StringDisplayRE($sStr, $iNumOfCol = 0)
    Return StringRegExpReplace(StringRegExpReplace($sStr, "(.*[^\v])(?:\v+)", "${1}" & @TAB), _
            "((.*?" & @TAB & "){" & $iNumOfCol & "})", "${1}" & @CRLF)
EndFunc   ;==>_StringDisplayRE


Func _StringDisplay2DTransposedRE($sStr, $iNumOfCol = 0)
    StringRegExpReplace($sStr, "(?:.*)(\v+)", "\1")
    Local $iNumOfLines = @extended, $sRes = ""
    If (($iNumOfLines / $iNumOfCol) - Int($iNumOfLines / $iNumOfCol)) = 0 Then
        For $x = 1 To $iNumOfCol
            $sRes &= StringRegExpReplace($sStr, "((.*[^\v])(?:\v+)){" & $x & "}((.*[^\v])(?:\v+)){" & ($iNumOfCol - $x) & "}", "${2}" & @TAB) & @CRLF
        Next
    Else
        $sRes = "Number of lines, " & $iNumOfLines & ", with " & $iNumOfCol & " columns will not display well." & @CRLF
    EndIf
    Return $sRes
EndFunc   ;==>_StringDisplayTransposeRE
;

I had to.

.........

Will we transform this in UDF?

jscript

There is really only the sort function, 1st post, and this one, that could go into a UDF.

The rest are one-liners using the AutoIt built-in command StringRegExpReplace.

So I lean towards the efficiency of using one-liners, as opposed to calling the same one-liner from within a function.

I can't imagine how a UDO-L, (User Defined One-Liner) file would work:)

Malkey

Just a thought. Auto-inclusion into SciTE's Snippet Holder, (Ctrl+Alt+s), could have some merit.

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...