• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Remove extra spaces from a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Remove extra spaces from a string


  • Subject: Re: Remove extra spaces from a string
  • From: Emmanuel <email@hidden>
  • Date: Sat, 7 Jun 2003 13:16:00 +0200

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.

  • Follow-Ups:
    • Re: Remove extra spaces from a string
      • From: James Cass <email@hidden>
    • Re: Remove extra spaces from a string
      • From: John Delacour <email@hidden>
References: 
 >Remove extra spaces from a string (From: Mátyás Ferenc Farkas <email@hidden>)
 >Re: Remove extra spaces from a string (From: Peter Fischer <email@hidden>)

  • Prev by Date: Re: Do Perl Script?
  • Next by Date: Scripting the Desktop Printer Manager
  • Previous by thread: Re: Remove extra spaces from a string
  • Next by thread: Re: Remove extra spaces from a string
  • Index(es):
    • Date
    • Thread