Jump to content
Sign in to follow this  
pillbug

smart string parser

Recommended Posts

pillbug

Hi,

I have written a smart string parser that passes in a string and then another string explaining that performs operations into splitting the string. It will make more sense with my code.

This looks for a *, if it sees this symbol the number following it designates unknown values to include. Therefore, this searches for string values with certain known and unknown data parts.

#include <String.au3>
#include <Array.au3>


; Problems:
; Only works if First character in variable is a constant;
; Must end in *
; We can't grab all between, look into _StringBetween
; Can't remove an area yet

; Examples:
;$test="R1321//9823743 R123iso2uod R138294 3R1AB123//9339"
; $finally=SmartString($test, "R1*5//*2*")
; =>R1321//98 & R1AB123//93

$date= "Dates are: 03/09/09, 12/05/08, and 05/21/09"
$finally= SmartString($date, " *2/*2/*2*")
;splits into dates " 03/09/09"
_ArrayDisplay($finally)

Func SmartString($string, $value); Values are Constants, *3 to right, ** all between constants, ~remove between
    $lengthofleftvalue=StringinStr($value, "*")-1; 
; First I need to split strings to left of first *
    $splitstringarray=StringSplit($string, StringLeft($value, $lengthofleftvalue), 1); Split everything that starts with first value, left of first *
    $arraysizestring=UBound($splitstringarray)-2; Returns the number of matches in a String that matches the left side
;_ArrayDisplay($splitstringarray)
    Local $FirstOccurance[$arraysizestring]
    $icounter=0
    for $i=0 to $arraysizestring-1
        $substringtrimmed=stringtrimleft($splitstringarray[$i+2],StringMid($value, $lengthofleftvalue+2, 1))
;msgbox(0,"",$substringtrimmed)
        $valuetrimmed=Stringtrimleft($value, $lengthofleftvalue + 2)
        ConsoleWrite($substringtrimmed)
        $rest=dumbString($substringtrimmed, $valuetrimmed)
        If stringlen($rest)>0 then
        
        $FirstOccurance[$icounter]=StringLeft($value, $lengthofleftvalue) & StringLeft($splitstringarray[$i+2], StringMid($value, $lengthofleftvalue+2, 1)) & $rest
        $icounter+=1
        EndIf
;ConsoleWrite($FirstOccurance[$i])
        
    next
;_ArrayDisplay($firstoccurance)

    $totalcounter=0
    for $i=0 to $icounter
    if stringlen($firstoccurance[$i])>0 Then $totalcounter+=1
    Next
    Local $totalarray[$totalcounter]
    $totalcounter=0
    for $i=0 to $icounter
    if stringlen($firstoccurance[$i])>0 Then
        $totalarray[$totalcounter]=$firstoccurance[$i]
        $totalcounter+=1
    EndIf
    Next
    return $totalarray


EndFunc

; Single array recursion of string
Func DumbString($string, $value)
;Msgbox(0,"",$string)
    $lengthofleftvalue=StringinStr($value, "*")-1; Need 2nd array, the first two are empty
    $leftsubstring=StringLeft($string, $lengthofleftvalue)
    $leftvalue=StringLeft($value, $lengthofleftvalue)
; First I need to split strings to left of first *
;Msgbox(0,"", $leftsubstring)
If $lengthofleftvalue=0 Then
    return ""
Else
        $substringtrimmed=stringtrimleft($string, $lengthofleftvalue+StringMid($value, $lengthofleftvalue+2, 1))
        $valuetrimmed=Stringtrimleft($value, $lengthofleftvalue + 2)
;msgbox(0,"",$substringtrimmed)
        If $leftsubstring=$leftvalue then
        $stringsection=StringLeft($string, StringMid($value, $lengthofleftvalue+2, 1)+$lengthofleftvalue) 
;msgbox(0,"", $stringsection)
        return  $stringsection & dumbString($substringtrimmed, $valuetrimmed)
        Else
        return ""
        EndIf
;ConsoleWrite($occurance)

EndIf

EndFunc
Edited by pillbug

Share this post


Link to post
Share on other sites
weaponx

This isn't generalized enough. You can do most of this in a couple lines with StringRegExp or split it with a function I wrote, _StringSplitRegExp.

Example 1:

$test = "R1321//9823743 R123iso2uod R138294 3R1AB123//9339"
$result = StringRegExp($test, "(R1.{1,5}//.{2})",3)
For $X = 0 to Ubound($result)-1
    ConsoleWrite("[" & $X & "]: " & $result[$X] & @CRLF)
Next

[0]: R1321//98

[1]: R1AB123//93

Example 2:

$test = "Dates are: 03/09/09, 12/05/08, and 05/21/09"
$result = StringRegExp($test, "\d{2}/\d{2}/\d{2}",3)
For $X = 0 to Ubound($result)-1
    ConsoleWrite("[" & $X & "]: " & $result[$X] & @CRLF)
Next

[0]: 03/09/09

[1]: 12/05/08

[2]: 05/21/09

Edited by weaponx

Share this post


Link to post
Share on other sites
pillbug

This isn't generalized enough. You can do most of this in a couple lines with StringRegExp or split it with a function I wrote, _StringSplitRegExp.

Example 1:

$test = "R1321//9823743 R123iso2uod R138294 3R1AB123//9339"
$result = StringRegExp($test, "(R1.{1,5}//.{2})",3)
For $X = 0 to Ubound($result)-1
    ConsoleWrite("[" & $X & "]: " & $result[$X] & @CRLF)
Next

Example 2:

$test = "Dates are: 03/09/09, 12/05/08, and 05/21/09"
$result = StringRegExp($test, "\d{2}/\d{2}/\d{2}",3)
For $X = 0 to Ubound($result)-1
    ConsoleWrite("[" & $X & "]: " & $result[$X] & @CRLF)
Next
Oo, thanks for showing me this. It would have saved/saves a lot of work. :P

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.