Re: Remove extra spaces from a string
Re: Remove extra spaces from a string
- Subject: Re: Remove extra spaces from a string
- From: James Cass <email@hidden>
- Date: Sun, 8 Jun 2003 20:01:41 -0400
Forgive me if this was covered already. If you just want to remove
extra spaces (or any repeating character, tab, etc) from a string, the
"tr" terminal command works pretty good. Here's an example with
gratuitous "display dialogs".
--
------------------------------------------------------------------------
-------------------
set withspaces to "Extra spaces."
display dialog "Before: " & withspaces
set withoutspaces to do shell script ("echo " & withspaces & " | tr -s
' '")
display dialog "After: " & withoutspaces
--
------------------------------------------------------------------------
-------------------
Regards,
Cassj
On Saturday, June 7, 2003, at 07:16 AM, Emmanuel wrote:
At 7:51 PM +0200 06/06/03, Peter Fischer wrote:
The Anarhcy lists a content of an FTP server in a file, what looks
like
this:
-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
I have to get the name of the file (last text item of... With a
space TID),
what works fine, and the creation date of the file. Between the
first three
columns there are multiple spaces. I get many "" strings, if I do a
repeat
loop with a space TID. How can I remove the extra spaces in AS or
Finder
from a string like this? I found a script for it in InDesign, but the
application keywords doesn't work anywhere else.
(sorry, I did not get the Original Poster right: is that Peter?)
If you want to spend lots of efforts and times parsing those kinds of
strings with basic AppleScript, that's your choice and you will find
here much helpful advice.
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>
"several spaces" is " +"
"several non-spaces" is "[^ ]+"
So "[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+" will match
any of the lines.
Prepending "^" (beginning of line) and appending "$" (end of line)
forces the pattern to match a whole line:
"^[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+$"
Now, to select one of the sub-patterns, you "group" it between
parenthesize and then you refer to it as "\\1" (first group), "\\2"
(second group) etc.
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
-----------------------------
Smile's "Enhanced Find Dialog" - a favorable tool to test and perform
regular expressions - offers a menu with all the metacharacters of the
regular expressions explained.
</mini-lecture on regular expressions>
Emmanuel
_______________________________________________
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.