• 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: Strip substring using do shell script and regex
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strip substring using do shell script and regex


  • Subject: Re: Strip substring using do shell script and regex
  • From: Yvan KOENIG <email@hidden>
  • Date: Wed, 28 Dec 2016 18:46:47 +0100


Le 28 déc. 2016 à 17:17, Axel Luttgens <email@hidden> a écrit :


Le 28 déc. 2016 à 16:52, debt  a écrit :

I’m trying to strip a substring of 2 digits following an asterisk (e.g. *67 or *82, etc.) but I’m running into problems.  Can someone see what I’m doing wrong here?

set clean_text to do shell script "echo " & quoted form of original_name & " | perl -pe ’s/\*\d{2}//g;'"

The regex works fine in Perl, so I’m not sure what’s wrong with the above.

Hello Marc,

You need to double the backslashes so that the AppleScript parser interpret them as a single backslash:

set original_name to "a*22b*44c"
set clean_text to do shell script "echo " & quoted form of original_name & " | /usr/bin/perl -pe 's/\\*\\d{2}//g;'"
--> "abc"

HTH,
Axel

Hello

As I am curious I compared these  two versions.

--[SCRIPT]
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set startDate to current application's NSDate's |date|()

repeat 100 times
set original_name to "a*22b*44c"
set clean_text to do shell script "echo " & quoted form of original_name & " | /usr/bin/perl -pe 's/\\*\\d{2}//g;'"
end repeat

set timeDiff to startDate's timeIntervalSinceNow()
display dialog "That took " & (-timeDiff as real) & " seconds."
--> "That took 2,649607002735 seconds."
--[/SCRIPT]

and

--[SCRIPT]
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on replaceThis:thePattern inString:theString usingThis:theTemplate
set theNSString to current application's NSString's stringWithString:theString
set theOptions to (current application's NSRegularExpressionDotMatchesLineSeparators as integer) + (current application's NSRegularExpressionAnchorsMatchLines as integer)
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:theOptions |error|:(missing value)
set theResult to theRegEx's stringByReplacingMatchesInString:theNSString options:0 range:{location:0, |length|:theNSString's |length|()} withTemplate:theTemplate
return theResult as text
end replaceThis:inString:usingThis:

set startDate to current application's NSDate's |date|()
repeat 100 times
set original_name to "a*22b*44c"
set clean_text to its replaceThis:"([*][0-9])([0-9])" inString:original_name usingThis:"" --> "abc"
end repeat

set timeDiff to startDate's timeIntervalSinceNow()
display dialog "That took " & (-timeDiff as real) & " seconds."
--> "That took 0,036499023438 seconds."
--[/SCRIPT]

If I made no error, the second one is 73 times faster than the first.


Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mercredi 28 décembre 2016 18:45:39



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Strip substring using do shell script and regex (From: debt <email@hidden>)
 >Re: Strip substring using do shell script and regex (From: Axel Luttgens <email@hidden>)

  • Prev by Date: Re: Strip substring using do shell script and regex
  • Next by Date: Re: Strip substring using do shell script and regex
  • Previous by thread: Re: Strip substring using do shell script and regex
  • Next by thread: Re: Strip substring using do shell script and regex
  • Index(es):
    • Date
    • Thread