Jump to content

"advanced" StringSplit-ting


Penny
 Share

Recommended Posts

$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

$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 by furrycow
Instant Lockerz Invite - www.instantlockerzinvite.co.uk
Link to comment
Share on other sites

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 by Penny
Link to comment
Share on other sites

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 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

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

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 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

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

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

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 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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...