Can someone help me to split a string
split("'B09', 'A07''B10', 'A07'", "''")
--> {"'B09'", "'A07'", "'B10'", "'A07'"}
to split(someText, delimiter)
set AppleScript's text item delimiters to delimiter
set someText to someText's text items
set AppleScript's text item delimiters to {""} --> restore delimiters to default value
return someText
end split
the result I get is: {“‘B09’, 'A07”, “B10’, ‘A07’”}
but I want it to be: {“‘B09’”, “‘A07’”, “‘B10’”, “‘A07’”}
You have to set send the delimiter argument ", ", not “‘’”
with a delimiter of two single quotes the script returns a list containing two items
{“‘B09’, 'A07”, “B10’, ‘A07’”}
It’s quite impossible to split the string using pure AppleScript without a repeat loop,
for example this could be a solution
split("'B09', 'A07''B10', 'A07'", "'")
--> {"'B09'", "'A07'", "'B10'", "'A07'"}
to split(someText, delimiter)
set AppleScript's text item delimiters to delimiter
set someText to someText's text items
set resultList to {}
repeat with anItem in someText
if (length of contents of anItem) = 3 then
set end of resultList to "'" & anItem & "'"
end if
end repeat
set AppleScript's text item delimiters to {""} --> restore delimiters to default value
return resultList
end split
Maybe this one may fit your needs :
split("'B09', 'A07''B10', 'A07'", "'")
--> {"'B09'", "'A07'", "'B10'", "'A07'"}
to split(someText, delimiter)
# Remove the embedded spaces in case there are consecutive ones
set AppleScript's text item delimiters to " "
set someText to someText's text items
set AppleScript's text item delimiters to ""
set someText to someText as text
# Remove the commas
set AppleScript's text item delimiters to ","
set someText to someText's text items
set AppleScript's text item delimiters to ""
set someText to someText as text
# Replace couples of delimiters by delimiter & return & delimiter
set AppleScript's text item delimiters to delimiter & delimiter
set someText to someText's text items
set AppleScript's text item delimiters to delimiter & return & delimiter
set someText to someText as text
set AppleScript's text item delimiters to {""} --> restore delimiters to default value
# Split according to the return char
return paragraphs of someText
end split
Yvan KOENIG (VALLAURIS, France) samedi 15 mars 2014 11:46:26
Hi, Yvan. That’s neat. I arrived at a shorter variant, but didn’t account for spaces”“‘A 07’” might be valid.
split("'B09', 'A07''B10', 'A07'", "'")
on split(someText, delimiter)
set AppleScript's text item delimiters to ¬
{delimiter & return & delimiter, delimiter & ", " & delimiter, delimiter & delimiter, delimiter}
set someText to (someText's text items as text)'s paragraphs 2 thru -2
end split
Edited to place all search terms together.
Hey Evista,
This is clearly a job for regular expressions.
Personally I’d prefer to use the Satimage.osax for simplicity and brevity:
set sioOutPut to find text "'[^']+'" in _text with regexp, all occurrences and string result
--> {"'B09'", "'A07'", "'B10'", "'A07'"}
But you can do it with a shell script and a little elbow grease:
set _text to "'B09', 'A07''B10', 'A07'"
set outPut to do shell script "perl -0777 -ne '
my @array = m!'\"'\"'[^'\"'\"']+'\"'\"'!g;
$, = \"¢\";
print @array;
' <<< " & (quoted form of _text)
set AppleScript's text item delimiters to "¢"
set outPut to text items of outPut
--> {"'B09'", "'A07'", "'B10'", "'A07'"}
If you’re on Mavericks you can use a user-library to add regex capability to your script:
http://macscripter.net/viewtopic.php?pid=168797#p168797
I looked at Stefan’s while I rolled my own.
set nl to splitOnTrickySingleTicks({"'B09', 'A07''B10', 'A07'", "''"})
log nl
--> *'B09', 'A07', 'B10', 'A07'*)
on splitOnTrickySingleTicks(L)
tell (a reference to my text item delimiters)
set {tids, contents of it} to {contents of it, "'"}
set {L, contents of it} to {text items of (L as text), tids}
end tell
repeat with i from 1 to L's length
if 3 > (item i of L)'s length then
set item i of L to missing value
else
set item i of L to "'" & item i of L & "'"
end if
end repeat
return L's text
end splitOnTrickySingleTicks
Edit
I didn’t notice the enveloping single quotes the first time around.
Hello
I’m back with a very simple scheme using no loop.
split("'B09', 'A07''B10', 'A07'", "'")
--> {"'B09'", "'A07'", "'B10'", "'A07'"}
to split(someText, delimiter)
# Split the original text (upon single quote + single quote AND single quote + comma + space + single quote)
set AppleScript's text item delimiters to {"''", "', '"}
set someText to someText's text items
# Paste the items with the delimiter (single quote + return + single quote)
set AppleScript's text item delimiters to "'" & return & "'"
set someText to someText as text
# Now you have the wanted items separated by return
set AppleScript's text item delimiters to {""} --> restore delimiters to default value
# That's all folks !
return paragraphs of someText
end split
If we remove the comments it becomes :
split("'B09', 'A07''B10', 'A07'", "'")
--> {"'B09'", "'A07'", "'B10'", "'A07'"}
to split(someText, delimiter)
set AppleScript's text item delimiters to {"''", "', '"}
set someText to someText's text items
set AppleScript's text item delimiters to "'" & return & "'"
set someText to someText as text
set AppleScript's text item delimiters to {""}
return paragraphs of someText
end split
Simple and neat.
Yvan KOENIG (VALLAURIS, France) dimanche 16 mars 2014 18:29:17