Jump to content
Sign in to follow this  
DicatoroftheUSA

Better split by regular expression.

Recommended Posts

DicatoroftheUSA

Here is a function. I am just looking for advice on how to make it more elegant/lean/robust.

The goal is to make a stringsplit type function, that uses a regular expression as a deliminator.

It returns an array containing what is between the deliminators, and [0]= delimitation string.

I tried writing it using stringsplit and _arrayconcanate but I had issues, with memory, with very large strings.

#include <Array.au3>

;~ ---for example
$sString="abc124bcd2cde3def4efg"

$sRegExp="[0-9]"
$aResult=_REsplit($sString,$sRegExp)
_arraydisplay($aresult)

$sRegExp="124"
$aResult=_REsplit($sString,$sRegExp)
_arraydisplay($aresult)

$sRegExp="[cd]"
$aResult=_REsplit($sString,$sRegExp)
_arraydisplay($aresult)
;~------

Func _RESplit($sString, $sRegExp)
    Local $aReturn[1]
    $aReturn[0]=$sRegExp

    $aSplits = StringRegExp($sString, $sRegExp, 3)

    For $x = 0 To UBound($aSplits) - 1
        if StringInStr($sString, $aSplits[$x]) then
            $iLeft = StringInStr($sString, $aSplits[$x])-1
            $iRight = StringLen($sString)-($iLeft+stringlen($aSplits[$x]))


            _ArrayAdd($aReturn, StringLeft($sString, $iLeft))
            $sString = StringRight($sString, $iRight)


        EndIf

    Next
    _ArrayAdd($aReturn, $sString)
    Return($aReturn)
EndFunc   ;==>_RESplit

Share this post


Link to post
Share on other sites
Mat

$aReturn = StringRegExp($sString, "(?:(.*?)" & $sRegExp & ")", 3)
_ArrayInsert($aReturn, 0, $sRegExp)

Does that do it? You may want to set some options for the regex...

Share this post


Link to post
Share on other sites
DicatoroftheUSA

$aReturn = StringRegExp($sString, "(?:(.*?)" & $sRegExp & ")", 3)
_ArrayInsert($aReturn, 0, $sRegExp)

Does that do it? You may want to set some options for the regex...

Thanks. but no, the added strings cause it to behave almost oppositely of what I desire.

The script is to work as stringsplit(), but to accept regular expressions as deliminators.

I am just looking for proper coding, idiot proofing, and code efficiency tips before I put it on the example scripts board to get flamed.

Edited by DicatoroftheUSA

Share this post


Link to post
Share on other sites
Mat

can you give an example of what you want the output to be then... I'm not sure I understand what you are trying to do... This gets exactly the same results as your function on all three of you examples, so unless your function isn't doing what you want it to do :blink:

Func _RESplit2($sString, $sRegExp)
    $aReturn = StringRegExp($sString, "(?:(.*?)(?:" & $sRegExp & "|\z))", 3)
    _ArrayInsert($aReturn, 0, $sRegExp)
    _ArrayPop($aReturn)

    Return $aReturn
EndFunc

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
Sign in to follow this  

×

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.