Re: Remove extra spaces from a string
Re: Remove extra spaces from a string
- Subject: Re: Remove extra spaces from a string
- From: Jean-Baptiste LE STANG <email@hidden>
- Date: Sun, 8 Jun 2003 09:57:37 +0200
An d of course a home made Applescript will work too. you can eihter
choose to do it recursively or not, and can pass a list of string too.
No as quick as other methods.
JB
<script>
on replace(inThisString, thisMatch, byThisString, recursively)
if class of inThisString is list then
repeat with x from 1 to count inThisString
set item x of inThisString to replace(item x of inThisString,
thisMatch, byThisString, recursively)
end repeat
return inThisString
else
set finalString to inThisString
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to thisMatch
set myTextItems to get every text item of finalString
set finalTextItemsList to {}
repeat with x from 1 to count myTextItems
if item x of myTextItems is not "" then set end of
finalTextItemsList to item x of myTextItems
end repeat
set AppleScript's text item delimiters to byThisString
set finalString to finalTextItemsList as string
if ((count (finalTextItemsList)) is not 1 and recursively) then set
finalString to replace(finalString, thisMatch, byThisString,
recursively)
set AppleScript's text item delimiters to oldTID
return finalString
end if
end replace
<script>
Le samedi, 7 juin 2003, ` 17:23 Europe/Paris, John Delacour a icrit :
At 1:16 pm +0200 7/6/03, Emmanuel wrote:
If you want to get the things done fast and efficiently and spend
your time on something else, then use the regular expressions, an
easy advanced string search language made available in AppleScript by
the (free) scripting addition "Satimage".
<mini-lecture on regular expressions>
[...]
Which gives, using Satimage osax' "find text" command and assuming x
stores the whole string:
-----------------------------
find text "^([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)
+([^ ]+) +([^ ]+)$" in x using {"\\1", "\\8"} with regexp, string
result and all occurrences
-- {{"-rwx------", "demo10.log"}, {"-rwx------", "demo11.log"},
{"-rwx------", "demo12.log"}, {"-rwx------", "demo13.log"},
{"-rwx------", "demo14.log"}}
-----------------------------
that you can rewrite in a more compact form, using the "{n}"
notation which repeats the sub-pattern located just before:
-----------------------------
find text "^([^ ]+) +([^ ]+ +){6}([^ ]+)$" in x using {"\\1", "\\3"}
with regexp, string result and all occurrences
-----------------------------
Or you could, of course, use perl ... nay, even perl -e :-)
set s to "-rwx------ 0 286 512 Jun 06 12:36
demo10.log
-rwx------ 0 286 512 Jun 06 12:37 demo11.log
-rwx------ 0 286 512 Jun 06 12:38 demo12.log
-rwx------ 0 286 512 Jun 06 12:39 demo13.log
-rwx------ 0 286 512 Jun 06 12:40 demo14.log"
set plscr to "for (split $/, " & quoted form of s & ") {
s~.+([A-S])~$1~g ;
s~ [^ ]+$~,$&~g ;
print qq~$_$/~ ; }"
do shell script "perl -e " & quoted form of plscr
--> "Jun 06 12:36, demo10.log
Jun 06 12:37, demo11.log
Jun 06 12:38, demo12.log
Jun 06 12:39, demo13.log
Jun 06 12:40, demo14.log"
.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.