Jump to content
Sign in to follow this  
Tankbuster

[solved] String split by pattern

Recommended Posts

Tankbuster

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

Share this post


Link to post
Share on other sites
FireFox

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.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
Tankbuster

Quick reply:

You are a :sorcerer: -of-code.

(and btw: In my third iteration of the code I got s similar solution but without the adding "}" again..... :shocked: )

Thank you, you saved my day....

Share this post


Link to post
Share on other sites
PhoenixXL

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.

Share this post


Link to post
Share on other sites
Tankbuster

@PhoenixXL

Yes, except the solution provided by FireFox I thought of a RegExp like this. But I missed it. And every time I lost my way in RegExp it's gone forever.....

Thank you all.

Share this post


Link to post
Share on other sites
Malkey

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)

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  

  • Similar Content

    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i receive a strange result from splitting strings elements contained in an array .(In attachment the resulting array)
      My Array contains strings like:
       
      INFO [26.01.2018 13:53:44] [XYZ] [XYZ, XYZ] Delta pack 5.2.25_17.01.2018 server version: 5.2.25_22.01.2018 not available. My code is:
      For $a=0 to UBound($Array_info_4) -1 $Array_info_4[$a]=StringSplit($Array_info_4[$a],",",$STR_NOCOUNT) Next I tried also using a second Array to store the result, but the result was the same, like in the attachment.
      Where is my mistake? :-)
       
       
       

    • kneze
      By kneze
      Hi
      i try to read Wlan Profiles from local Computer. First Step Command Prompt: netsh wlan show profiles >> C:\temp\test\wlan.txt.
      Second Step (here's a sample script to find a solution for my problem. Script read each line of wlan.txt and Display Name of Wireless Connection which i have set up in the past.  If i edit wlan.txt to wlan - sample.txt script works. wlan.txt containsline without : so i get error message. How can i read only lines beginning with All User Profile so i can use original file wlan.txt which i crate in the command prompt with: netsh wlan show profiles >> C:\temp\test\wlan.txt ?
      Thanks for any suggestions.
      Kneze
      $Form1 = GUICreate("Form1", 374, 268, 892, 512) $Button1 = GUICtrlCreateButton("button", 145, 35, 75, 25) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $file = "C:\temp\test\wlan.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) $after = StringSplit($line, ":")[2] MsgBox(262144, "Result", $after, 0) Next FileClose($file) EndSwitch WEnd  
       
      wlan - sample.txt
      wlan.txt
    • blemas
      By blemas
      The McAfee return for AV defs per "C:\Progra~1\Common~1\McAfee\SystemCore\csscan.exe -Versions" is ...
      CommonShell Command Line Scanner Lite (VSCORE.15.5.0.3960)
          Engine version: 5800.7501
          DAT version:    8450.0
          Time required:  15 milliseconds
      I want to isolate the actual DAT version as "8450.0".  There may be an easier way to get the DAT Version via other McAfee or registry methods but essentially I just want to know how to parse a string at a character or @CRLF into two separate strings for further parsing. Example:
      $string = "Name=Microsoft Windows 10 Professional |C:\windows|\Device\Harddisk0\Partition2"
      Parse into $var1 = "Name" & $var2 = "Microsoft Windows 10 Professional |C:\windows|\Device\Harddisk0\Partition2"
      From there I'd like to parse $var2 at "|" into  $var3 = "Microsoft Windows 10 Professional" and $var4 "C:\windows|\Device\Harddisk0\Partition2"
       
       
       
       
       
    • crushyna
      By crushyna
      Hello everyone!

      I've encountered a strange problem while adding files (or rather file names) to a ListView.
      Mechanics are simple:
      1. Select files to add (FileOpenDialog + Multiselect)
      2. Split selected string (filepath) into array (StringSplit)
      3. File names appear on ListView (GUICtrlCreateListViewItem)

      Writing starts from second array (since 0 is array size, and 1 is filepath).
      Problem occurs when I try to add one file. Then, no filenames are added.
      As far as I know, selecting one file generates only 2 strings (0 and 1) to split, therefore no string is written into ListView.
       
      Is there any way / workaround to make this thing work properly?

      Some code here:
      While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $idAddTest $sMessage = "Select test cases to load:" $sFileOpenDialog = FileOpenDialog($sMessage, @WorkingDir & "\data\tests\", "All (*.*)", $FD_FILEMUSTEXIST & $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No test(s) were selected.") FileChangeDir(@ScriptDir) Else FileChangeDir(@ScriptDir) $aFileList = StringSplit($sFileOpenDialog, "|", @CRLF) For $i = 2 to $aFileList[0] MsgBox( 0, "test", $aFileList[$i]) ;just for testing purposes GUICtrlCreateListViewItem($aFileList[$i], $idTestCaseList) Next EndIf EndSwitch WEnd  
      And nothing more spectacular from ListView side:
      Global $idTestCaseList = GUICtrlCreateListView("List of Tests ", 24, 24, 225, 502, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT)) GUICtrlSetTip(-1, "List of test cases."& @CRLF &"Hold CTRL to select multiple lines.")  
      Any help appreciated!
    • ur
      By ur
      How to use regular expressions in StringSplit and StringInStr
×