Jump to content

[solved] String split by pattern


Recommended Posts

Hello,

I'm lost again....(as always :sweating: )

I tried different approaches, _StringBetween, StringRegExp, StringInStr.... but in the end non of my approaches worked fine. Every time I ended up like this: :mad2:

As I don't want to confuse you with my now messed up code snippets, I will explain what I got and what I need (sorry for that, but I don't want someone to fix my wrong approach if there is a already working solution (btw. of course I read the forum for some hints, and there are a lot of them but non of the described hints worked for me in the end. Maybe I'm not smart enough these days to understand.....(please don't say yes here)))

So here we go:

My Input string is:

$sString='{"a1-a1":{"d1":"v1","d2":"v2"},"b1-b1":{"d3":"v3","d4":"v4"},"c1-c1":{"d5":"v5","d6":"v6"}}'

this means I got three "fields" with the pattern:

"<prefix always 2char-2char with quotes>":{<value1 with data1 in quotes>,<value1 with data1 in quotes>}

separated by a " , ", surrounded by a "{}" pair.

I want to retrieve an array like this:

$arrTemp[1]="a1-a1":{"d1":"v1","d2":"v2"}
$arrTemp[2]="b1-b1":{"d3":"v3","d4":"v4"}
$arrTemp[3]="c1-c1":{"d5":"v5","d6":"v6"}

optional also with the start and end position of that string based on the original string in the same array.

eg: $arrTemp[1][0]="a1-a1":{"d1":"v1","d2":"v2"}

$arrTemp[1][1]=3

$arrTemp[1][2]=31

I "guess" this could be achieved by a StringRegExp formatted command. I wanted to define a pattern that split up the string into fields.

Could some one help me out?

Edited by Tankbuster
Link to comment
Share on other sites

Hi,

Quick answer with StringSplit :

#include <Array.au3>

Global $sString, $sStripped, $a, $i

$sString = '{"a1-a1":{"d1":"v1","d2":"v2"},"b1-b1":{"d3":"v3","d4":"v4"},"c1-c1":{"d5":"v5","d6":"v6"}}'

$sStripped = StringMid($sString, 2, StringLen($sString) - 2)

$a = StringSplit($sStripped, "},", 1)

For $i = 1 To $a[0] - 1
$a[$i] &= "}"
Next

_ArrayDisplay($a)

Br, FireFox.

Link to comment
Share on other sites

I tried different approaches, _StringBetween, StringRegExp, StringInStr....

here is an RegEx version
#include <Array.au3>

$String='{"a1-a1":{"d1":"v1","d2":"v2"},"b1-b1":{"d3":"v3","d4":"v4"},"c1-c1":{"d5":"v5","d6":"v6"}}'

;Delete the starting and ending braces
$String = StringRegExpReplace( $String, "(^{|}$)", '' )

Local $aRay = StringRegExp( $String, '([^}]+})(?:,|$)', 3 )
_ArrayDisplay( $aRay )

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

I wouldn't call this string splitting.

It's more like matching sub-strings.

#include <Array.au3>

Local $String = '{"a1-a1":{"d1":"v1","d2":"v2"},"b1-b1":{"d3":"v3","d4":"v4"},"c1-c1":{"d5":"v5","d6":"v6"}}'

; The RE pattern will match all sub-strings that start with a double quote,'"', followed by everything
; that is not a dash,'-', up to a dash, and then captures everything up to, and including the first '}'
; encountered travelling from left to right. That first '}' encountered is the end of each sub-string
; to be captured.
Local $aArray = StringRegExp($String, '"[^\-]+-.+?}', 3)

_ArrayDisplay($aArray)
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

×
×
  • Create New...