Hi Marcus,mtettmar wrote:Here's the RegEx solution:
So full code to create an array from a line of CSV and loop through each field:
Code: Select all
//Example CSV line Let>text=sally,"1,2500",fred //RegEx to extract each field to an array Let>pattern="[^"\r\n]*"|[^,\r\n]* RegEx>pattern,text,0,fields,num,0 //Loop through each field Let>k=0 Repeat>k Let>k=k+1 Let>the_field=fields_%k% //RegEx to remove starting and ending quotes if present RegEx>^"|"$,the_field,0,matches,n,1,,the_field //Display the field MessageModal>the_field Until>k=num
Thanks again for the above RegEx solution... its like Separate> on steroids.
I translated your RegEx into an EasyPattern and it works just as well. I even added some more challenging data like empty values ,,, (nothing between successive commas) and an empty double quoted value ,"", and both your original and the one below performed as expected:
Code: Select all
//Example CSV line
Let>text=sally,"1,2500",fred,"111,222,333",,"",red,green,done
//EasyPatterns translation
Let>pattern=[('"' (zeroOrMore (not doubleQuote or return or linefeed)) '"') or (zeroOrMore (not comma or return or linefeed))]
RegEx>pattern,text,1,fields,num,0
//Loop through each field
Let>k=0
Repeat>k
Let>k=k+1
Let>the_field=fields_%k%
//RegEx to remove starting and ending quotes if present
RegEx>^"|"$,the_field,0,matches,n,1,,the_field
//Display the field
MessageModal>the_field
Until>k=num
Code: Select all
//Example CSV line
Let>text=sally,"1,2500",fred,"111,222,333",,"",red,green,done
//EasyPatterns - trying to use mustBeginWith and mustEndWith to discard the double quotes
Let>pattern=[(mustBeginWith(doubleQuote) (zeroOrMore (not doubleQuote or return or linefeed)) mustEndWith(doubleQuote)) or (zeroOrMore (not comma or return or linefeed))]
RegEx>pattern,text,1,fields,num,0
//Loop through each field
Let>k=0
Repeat>k
Let>k=k+1
Let>the_field=fields_%k%
//RegEx to remove starting and ending quotes if present
//RegEx>^"|"$,the_field,0,matches,n,1,,the_field
//Display the field
MessageModal>the_field
Until>k=num
That way, I hoped to discard the leading and trailing double quotes... without having to use a second RegEx to do it later in the loop.
Well it doesn't work. If you run the example above, you'll see all kinds of strange matches going on. I am wondering though... is it because there's a problem with how I've written the EasyPattern... or is discarding the double quotes in this way just not possible?