Re: Parsing quoted text
Re: Parsing quoted text
- Subject: Re: Parsing quoted text
- From: T&B <email@hidden>
- Date: Wed, 27 Sep 2006 13:22:49 +1000
Hi Yvan,
Important question.
Are the datas allowed to contain this kind of values:
item1 item2 'item [3]' [item's 4] "item's [5]"
I think to that because it may be the reason explaining the use of several delimiters.
That's correct. So if the quotePairList contains "" '' and [] then the output should be:
{"item1", "item2", "item [3]", "item's 4", "item's [5]"}
or, as per my original post:
item1 item2 'item 3' 'item 4' [item 5] "item[6]" 'item "7"'
parsed using a delimiter of: a space
and a list of quote pairs: "" '' []
Would give:
{"item1", "item2", "item 3", "item 4", "item 5", "item[6]", "item \"7\""}
Or, for an everyday example:
osascript -e 'tell application "iChat" to set status message to "AppleScripting"'
parsed by space delimiter and quotes "" and '', would give:
--> {"osascript", "-e", "tell application \"iChat\" to set status message to \"AppleScripting\""}
and parsing the last item of that result would give:
--> {"tell", "application", "iChat", "to", "set", "status", "message", "to", "AppleScripting"}
or, if parsing a shell SQL statement:
sqlite3 test.db 'CREATE TABLE [Customer List]([First name] TEXT, [Last name] TEXT, id INTEGERPRIMARY KEY AUTOINCREMENT, status TEXT DEFAULT \"Current\")'
with delimiter = space, quotePairList = "" ''
gives:
{"sqlite3", "test.db", "CREATE TABLE [Customer List]([First name] TEXT, [Last name] TEXT, id INTEGER AUTOINCREMENT PRIMARY KEY, status TEXT DEFAULT \"Current\")"}
And further on, using the field definitions to parse again:
set textBlock to "[First name] TEXT, [Last name] TEXT, id INTEGERPRIMARY KEY AUTOINCREMENT , status TEXT DEFAULT \"Current\""
set delimiter to ","
set quotePairList to {} -- ie none
set doTrimSpace to true -- ie remove white space (spaces and tabs) surrounding each parsed item
set columnDefList to ParseQuotedText of textBlock between delimiter out of quotePairList with doTrimSpace
--> {"[First name] TEXT", "[Last name] TEXT", "id INTEGER PRIMARY KEY AUTOINCREMENT", "status TEXT DEFAULT \"Current\""}
--Then parsing each of those items using:
set delimiter to " "
set quotePairList to {{quote, quote}, {"'", "'"}, {"[", "]"}}
set doTrimSpace to true
set tableDefList to {}
repeat with columnDef in columnDefList
set end in tableDefList to ParseQuotedText of columnDef between delimiter out of quotePairList with doTrimSpace
end repeat
return tableDefList
--> {
{"First name", "TEXT"},
{"Last name", "TEXT"},
{"id", "INTEGER", "PRIMARY", "KEY", "AUTOINCREMENT"},
{"status", "TEXT", "DEFAULT", "Current"}
}
I hope this gives a thorough background. Thanks for your input. It seems like a common requirement, to parse with this level of flexibility. Has no-one else come across such a need? I'll post my own solution soon, if anyone else would like it.
Thanks,
Tom
T&B
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden