Re: NSRegular expression question
Re: NSRegular expression question
- Subject: Re: NSRegular expression question
- From: Jim Weisbin <email@hidden>
- Date: Sat, 2 Sep 2017 09:52:26 -0400
Also, what’s the best way to strip the extension from the filename? Does it
make sense to use a regular expression to do that, or is there a more direct
method? Currently I’m doing this, which I suppose is the AppleScript way to do
it.
set extName to (get name extension of theFile)
set extLength to count extName
set fileName to (get name of theFile)
set newName to text 1 thru -(extLength + 2) of fileName
> On Sep 2, 2017, at 9:42 AM, Jim Weisbin <email@hidden> wrote:
>
> Thanks!
>
> Shane Stanley <email@hidden
> <mailto:email@hidden>> wrote:
>>
>> There's a special method for getting the first match, which is effectively
>> what you're after: firstMatchInString:options:range:. In fact, there's
>> another for getting the range directly:
>>
>> use AppleScript version "2.4" -- Yosemite (10.10) or later
>> use framework "Foundation"
>> use scripting additions
>>
>> set theText to current application's NSString's
>> stringWithString:"Human_012345_something_123456"
>> set myRegex to current application's NSRegularExpression's
>> regularExpressionWithPattern:"(\\d){6}$" options:0 |error|:(missing value)
>> set matchRange to myRegex's rangeOfFirstMatchInString:theText options:0
>> range:{0, theText's |length|()}
>> set theWord to (theText's substringWithRange:matchRange) as text
>> display dialog theWord as text
>>
>> Or even simpler:
>>
>> use AppleScript version "2.4" -- Yosemite (10.10) or later
>> use framework "Foundation"
>> use scripting additions
>>
>> set theText to current application's NSString's
>> stringWithString:"Human_012345_something_123456"
>> set matchRange to theText's rangeOfString:"(\\d){6}$" options:(current
>> application's NSRegularExpressionSearch)
>> set theWord to (theText's substringWithRange:matchRange) as text
>> display dialog theWord as text
>>
Jim Weisbin | C.T.O. | Human | Post Human | 27 West 20th Street | Suite 801 |
New York, NY | 10011 | (212) 352-0211 | (917) 375-2272 | 2046 Broadway |
Santa Monica, CA | 90404 | (310) 264-0211 telephone | www.humanworldwide.com
<http://www.humanworldwide.com/>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
applescriptobjc-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden