Re: NSRegular expression question
Re: NSRegular expression question
- Subject: Re: NSRegular expression question
- From: Shane Stanley <email@hidden>
- Date: Sat, 2 Sep 2017 22:46:02 +1000
On 2 Sep 2017, at 10:29 pm, Jim Weisbin <email@hidden> wrote:
>
> Since I am only expecting one match, it seems like I should be able to
> eliminate the repeat loop
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
--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.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