Jump to content

StringRegExp pattern equivalent to stringsplit


Deye
 Share

Recommended Posts

i'm wanting to create a function that will accept a delimiter and a regx pattern and return an array based on these

need some better ideas on how to start building it

thanks

#include <Array.au3>

$s = ""
For $i = 1 To 1000
    $s &= "|" & Round(Random(0, 15), 2)     ;example A
;~  $s &= @CRLF & Round(Random(0, 15), 2) ;example B
Next

;~ _ArrayDisplay(ArrRegExp($s, "|", "\d.*[.].*"))       ;example A
_ArrayDisplay(ArrRegExp($s, "|", ".*"))     ;example A
;~ _ArrayDisplay(ArrRegExp($s, "R", ".*"))      ;example B


Func ArrRegExp($sString = "", $delim = ",", $RegExpPat = ".*")
    Local $sDelim = "\" & $delim
    $sRegExpPat = $RegExpPat & '?(?=' & $sDelim & ')'
    Return StringRegExp($s, $sRegExpPat, 3)
EndFunc   ;==>ArrRegExp

 

Edited by Deye
Link to comment
Share on other sites

42 minutes ago, Deye said:

need some better ideas on how to start building it

Match everything except the delimiter char.
In RegEx (PCRE) you can define negative char groups with [^...]

Edit: Ah I have now understood what you actually want.
In principle you could put this into a single pattern but whether it actually works as desired still depends on the pattern in $RegExpPat. You can't treat this pattern as you might think.
For example, in your example you would have to make sure that the quantifier is lazy, otherwise everything up to the end of the string would be matched:
 

#include <Array.au3>

$s = ""
For $i = 1 To 1000
    $s &= "|" & Round(Random(0, 15), 2)
Next

_ArrayDisplay(ArrRegExp($s, "|", ".*?"))

Func ArrRegExp(ByRef $sString, $delim = ",", $RegExpPat = ".*")
    Return StringRegExp($sString, '(?<=\Q' & $delim & '\E)' & $RegExpPat & '(?=\Q' & $delim & '\E|\Z)', 3)
EndFunc

Therefore it should be the cleanest variant to first separate everything and then use the second Pattern to filter the results:
 

#include <Array.au3>

$s = ""
For $i = 1 To 1000
    $s &= "|" & Round(Random(0, 15), 2)
Next

_ArrayDisplay(ArrRegExp($s, "|", ".{5}"), Example)

Func ArrRegExp(ByRef $sString, $delim = ",", $RegExpPat = ".*")
    Local $aVals = StringRegExp($sString, '[^\Q' & $delim & '\E]+|(?<=\Q' & $delim & '\E|\A).{0}', 3)
    Local $aRet[UBound($aVals)], $cCount = 0

    For $sVal In $aVals
        If StringRegExp($sVal, $RegExpPat) Then
            $aRet[$cCount] = $sVal
            $cCount += 1
        EndIf
    Next
    Redim  $aRet[$cCount]

    Return $aRet
EndFunc

 

Edited by AspirinJunkie
Link to comment
Share on other sites

I'm towards using it in a more open manner so when i use StringRegExp i will get the final array based on the pattern used

#include <Array.au3>
$s = ""
For $i = 1 To 100
    $s &= "|" & Round(Random(0, 15), 7)      ;example A
Next

_ArrayDisplay(ArrRegExpUnique($s, "|", "\d{2}"))
_ArrayDisplay(ArrRegExpUnique($s, "|", "\d{2}[.].{3}"))

$s = StringReplace($s, "|", @CRLF) ;replacing delimiter to lines @CRLF
_ArrayDisplay(ArrRegExpUnique($s, "R", "\d{1}[.].{1}")) ;using "R" as the delimiter
_ArrayDisplay(ArrRegExpUnique($s, "R", "\d{2}[.].{1}"))
_ArrayDisplay(ArrRegExpUnique($s, "R", "\d{2}[.].{4}")) 

Func ArrRegExpUnique($sString, $delim = ",", $RegExpPat = ".*?")
    Local $bLF = (StringRegExp($delim, "(R|n)") ? True : False)
    $sRegExpPat = ($bLF ? "(*CRLF)" : '(?<=\' & $delim & ")") & $RegExpPat
    ConsoleWrite($sRegExpPat & @LF)
    Local $a = _ArrayUnique(StringRegExp(($bLF ? @CRLF : $delim) & $sString, $sRegExpPat, 3))
    _ArraySort($a, 0, 1)
    Return $a
EndFunc

 

Edited by Deye
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...