Penny Posted September 28, 2009 Share Posted September 28, 2009 $st = '"Jesus, the Reckless",0,0,M,ENG-US,298,No' $arr = StringSplit($st,",") how can I make a similar use of StringSplit to keep the field between "" together? e.g so the final result is $arr[0] = 7 $arr[1] = '"Jesus, the Reckless"' $arr[2] = '0' $arr[3] = '0' $arr[4] = 'M' $arr[5] = 'ENG-US' $arr[6] = '298' $arr[7] = 'No' similar ideas I had were much longer and I was wondering if there is a simpler way to do it before I delve into creating long and perhaps innecessary code. Link to comment Share on other sites More sharing options...
TurionAltec Posted September 28, 2009 Share Posted September 28, 2009 Are they always going to happen in this format, where only the first element would be this special case? Link to comment Share on other sites More sharing options...
furrycow Posted September 28, 2009 Share Posted September 28, 2009 (edited) $st = '"Jesus, the Reckless",0,0,M,ENG-US,298,No' $arr = StringSplit($st,",") how can I make a similar use of StringSplit to keep the field between "" together? e.g so the final result is $arr[0] = 7 $arr[1] = '"Jesus, the Reckless"' $arr[2] = '0' $arr[3] = '0' $arr[4] = 'M' $arr[5] = 'ENG-US' $arr[6] = '298' $arr[7] = 'No' similar ideas I had were much longer and I was wondering if there is a simpler way to do it before I delve into creating long and perhaps innecessary code. Just before i have a proper look at it...is this a one off string, or are there more strings similar, eg..some with a comma within the "" and some without? So it is possible to see what angle to approach it. Thanks EDIT: Ha, great minds. Edited September 28, 2009 by furrycow Instant Lockerz Invite - www.instantlockerzinvite.co.uk Link to comment Share on other sites More sharing options...
Penny Posted September 28, 2009 Author Share Posted September 28, 2009 (edited) Sometimes you would have '"Jesus, the Reckless"' and most of the time you would have 'Motorbike', but this only happens in the first "element". the general rule is if it's a string that has a ',' in it that isn't the separator, then it's quoted. Edited September 28, 2009 by Penny Link to comment Share on other sites More sharing options...
memoryoverflow Posted September 28, 2009 Share Posted September 28, 2009 (edited) Nothing advanced:$st = '"Jesus, the Reckless",0,0,M,ENG-US,298,No' $st = StringRegExpReplace ($st, '["](.*)[,](.*)["]', '"${1}§${2}"') ; save ',' to '§' if between doublequotes $arr = StringSplit($st, ',') ; seperate by ',' $arr[1] = StringRegExpReplace ($arr[1], '["](.*)[§](.*)["]', '"${1},${2}"') ; restore ',' from '§'last line assuming that only the first part may contain actual text - otherwise use a loop for the arry elements. You can do it shorter, but this might be more transparent. The placeholder character is your choice - just take one that will never appear in your text. Edited September 28, 2009 by memoryoverflow (The signature is placed on the back of this page to not disturb the flow of the thread.) Link to comment Share on other sites More sharing options...
Penny Posted September 28, 2009 Author Share Posted September 28, 2009 Nothing advanced:$st = '"Jesus, the Reckless",0,0,M,ENG-US,298,No' $st = StringRegExpReplace ($st, '["](.*)[,](.*)["]', '"${1}§${2}"') ; save ',' to '§' if between doublequotes $arr = StringSplit($st, ',') ; seperate by ',' $arr[1] = StringRegExpReplace ($arr[1], '["](.*)[§](.*)["]', '"${1},${2}"') ; restore ',' from '§'last line assuming that only the first part may contain actual text - otherwise use a loop for the arry elements. You can do it shorter, but this might be more transparent. The placeholder character is your choice - just take one that will never appear in your text. Oh I didn't know about StringRegExpReplace, what a wonderful function. Though the script takes forever to run, but that's because I'm looping through a 800k array over and over, thanks a bunch! Greatly appreciated. Link to comment Share on other sites More sharing options...
Hawkwing Posted September 29, 2009 Share Posted September 29, 2009 (edited) Why do all the extra stuff? I know my stringregexp patterns may not be the best since I'm still kinda noob at it, but this just seems a whole lot more efficient to me. Especially since it's being looped through an 800k array repeatedly. #include<array.au3> $string = '"Jesus, the Reckless",0,0,M,ENG-US,298,No' $array = StringRegExp($string, '(?:"[^"]{1,}"){0,},([^,]{1,})', 3) MsgBox(0, "", @error & @CRLF & @extended) _ArrayDisplay($array) Edited September 29, 2009 by Hawkwing The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again. Link to comment Share on other sites More sharing options...
memoryoverflow Posted September 29, 2009 Share Posted September 29, 2009 Sure, that similar to my "shorter" approach. Wonder whether Penny can debug it. I can, but I wanted to give a more "transparent" picture. (The signature is placed on the back of this page to not disturb the flow of the thread.) Link to comment Share on other sites More sharing options...
Penny Posted September 29, 2009 Author Share Posted September 29, 2009 I'm using this function, derived from what overflow posted here Func StringSplitA($strA,$delimiters,$flag=0,$tempDelimiter=";") Local $elem = StringRegExpReplace($strA, '["](.*)[' & $delimiters & '](.*)["]', '"${1}' & $tempDelimiter &'${2}"') Local $split_elem = StringSplit($elem,$delimiters,$flag) For $i = 1 to $split_elem[0] $split_elem[$i] = StringRegExpReplace($split_elem[$i], '["](.*)[' & $tempDelimiter & '](.*)["]', '"${1},${2}"') ;Restore Next Return $split_elem EndFunc Hawk I tried using your script (tbh I just pasted it and ran it) but the 'first element' doesn't even appear (teh 'jesus, the reckless thing') Link to comment Share on other sites More sharing options...
Mison Posted September 29, 2009 Share Posted September 29, 2009 Try this... #include<array.au3> $string = '"Jesus, the Reckless",0,0,M,ENG-US,298,No' $array = StringRegExp($string, '".*?"|(?<=,)(.*?)(?=,|\z)', 3) _ArrayDisplay($array) The last time I tried it, it worked Hi ;) Link to comment Share on other sites More sharing options...
Hawkwing Posted September 29, 2009 Share Posted September 29, 2009 I'm using this function, derived from what overflow posted here Func StringSplitA($strA,$delimiters,$flag=0,$tempDelimiter=";") Local $elem = StringRegExpReplace($strA, '["](.*)[' & $delimiters & '](.*)["]', '"${1}' & $tempDelimiter &'${2}"') Local $split_elem = StringSplit($elem,$delimiters,$flag) For $i = 1 to $split_elem[0] $split_elem[$i] = StringRegExpReplace($split_elem[$i], '["](.*)[' & $tempDelimiter & '](.*)["]', '"${1},${2}"') ;Restore Next Return $split_elem EndFunc Hawk I tried using your script (tbh I just pasted it and ran it) but the 'first element' doesn't even appear (teh 'jesus, the reckless thing') Sorry, my mistake. Forgot that you wanted to capture the first part as well. Just take out the ?: after the first parentheses. The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again. Link to comment Share on other sites More sharing options...
memoryoverflow Posted October 3, 2009 Share Posted October 3, 2009 (edited) But this has the same disadvantages as my proposal. Mison's is the correct one and I wouldn't have hesitated to post it to a regexe-newbie's question if... - I'd known that it would have to loop through a big text - I'd have found the very nice regexe tutorial in AutoIt's help earlier, which is suitable to avoid the necessity for long winded explanations coming from posting more advanced patterns. (My compliments on that to the Help authors.) *oops* mixed up the quote in above post with the pattern Hawkwing's posted earlier. Still, "my" "more advanced" pattern is exactly the one of Mison's post. Edited October 3, 2009 by memoryoverflow (The signature is placed on the back of this page to not disturb the flow of the thread.) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now