AppleScript名:正規表現で数字を抽出(NSRegularExpressionSearch) |
— Created 2017-12-17 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "126 ひよこ豆コーヒー豆だだちゃ豆" set n1Res to filterNumStr(aStr) of me log result –> "126" set aStr to "ひよこ豆コーヒー豆だだちゃ豆 345" set n2Res to filterNumStr(aStr) of me log result –> "345 set aStr to "ひよこ豆コーヒー豆999だだちゃ豆" set n3Res to filterNumStr(aStr) of me log result –> 999 on filterNumStr(aStr as string) set aLen to length of aStr set regStr to "\\d{1," & (aLen as string) & "}" set aRes to findStrByPattern(aStr, regStr) of me return aRes as {boolean, number} end filterNumStr on findStrByPattern(aText as string, regStr as string) set anNSString to current application’s NSString’s stringWithString:aText set aRange to anNSString’s rangeOfString:regStr options:(current application’s NSRegularExpressionSearch) if aRange = {location:0, length:0} then return "" set bStr to anNSString’s substringWithRange:aRange return bStr as string end findStrByPattern |
More from my site
(Visited 113 times, 2 visits today)