在 AppleScript 中实现 Regex 以精确匹配 6 个数字
Implementing Regex in AppleScript for matching exactly 6 numbers
我是 Regex 和 AppleScript 的新手,需要一些支持和指导。
第一个用户输入了一个字符串。它可以是一行或多行中的任何内容。
应在字符串上应用正则表达式以查找只有 6 位数字的数字..不多也不少,并用 space.
分隔它们
最终字符串应如下所示:867689, 867617, 866478, 866403, 866343
.
然后这个字符串将被转换成一个列表。
我正在使用这个网站来测试我的正则表达式:https://www.freeformatter.com/regex-tester.html
正好匹配6位数字的正则表达式是:
(?<!\d)\d{6}(?!\d)
我知道为了对 AppleScript 实施正则表达式,我需要使用 Shell 脚本。我也知道我应该使用 sed
但不幸的是我不完全知道如何使用它以及它到底是什么。
通过一些指南和测试,我了解到 sed 不适用于 \d
,我应该改用 [0-9]
,我也应该像这样 \(..\)
转义括号。另外替换 ,
应该像 ,
一样实现。直到这一刻我无法让它工作。
我的测试用户输入如下:
MASTER
ARTIKEL
Artikel
5910020015
867689
PULL1/1
5910020022
867617
PULL1/1
Cappuccino
5910020017
866478
PULL1/1
Braun
5921020017
866403
SHIRT1/2
Kastanie-Multi
5910020016
866343
PULL1/1
和 AppleScript 代码本身:
use scripting additions
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
on list2string(theFoldersList, theDelimiter)
set theBackup to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theString to theFoldersList as string
set AppleScript's text item delimiters to theBackup
return theString
end list2string
on run {input}
display dialog "Please enter your string: " default answer ""
set stringOfNumbers to the text returned of the result
set num to do shell script "sed 's/\(\(?<![0-9]\)[0-9]{6}\(?![0-9]\)\), /' <<< " & quoted form of stringOfNumbers
--(?<!\d)\d{6}(?!\d)
display dialog stringOfNumbers
set stringOfNumbers to current application's NSString's stringWithString:stringOfNumbers
set listOfArtNumbers to (stringOfNumbers's componentsSeparatedByString:", ") as list
display dialog list2string(listOfArtNumbers, ", ")
return input
end run
不幸的是,我在任何地方使用 \
转义字符时都会出错。所以我不得不删除所有 \
但是一旦我 运行 我收到的脚本 "Syntax Error: sed: 1: "s/(?<![0-9])[0-9]{6}(?! ...": unterminated substitute pattern"
我所有的努力都导致了类似的错误。
AppleScript Objective-C 允许我们使用 NSRegularExpression
做正则表达式,从 OS 10.7 (Lion) 开始。以下处理程序 returns 正则表达式搜索结果列表:
use AppleScript version "2.4"
use framework "Foundation"
property NSRegularExpression : class "NSRegularExpression"
property NSString : class "NSString"
on findPattern:thePattern inString:theString
set theText to NSString's stringWithString:theString
set theRegEx to NSRegularExpression's regularExpressionWithPattern:thePattern ¬
options:0 |error|:(missing value)
set theResult to (theRegEx's matchesInString:theText ¬
options:0 ¬
range:{location:0, |length|:theText's |length|})'s valueForKey:("range")
set outputArray to {}
repeat with thisRange in theResult
copy (theText's substringWithRange:thisRange) as text to end of outputArray
end repeat
return outputArray
end findPattern:inString:
请注意,'¬' 符号是一个 line-continuation 符号(在 AppleScript 编辑器中键入 option-return)。我分解了多行以使脚本更具可读性,但这可能 copy/paste 不正确,因此请注意这些应该是单一的、连续的行。
您按如下方式使用此处理程序。请记住,反斜杠在 AppleScript 中是一个特殊字符,因此必须通过在它前面加上另一个反斜杠来转义它:
set foundList to my findPattern:"(?<!\d)\d{6}(?!\d)" inString:"MASTER
ARTIKEL
Artikel
5910020015
867689
PULL1/1
5910020022
867617
PULL1/1
Cappuccino
5910020017
866478
PULL1/1
Braun
5921020017
866403
SHIRT1/2
Kastanie-Multi
5910020016
866343
PULL1/1"
-- Result: {"867689", "867617", "866478", "866403", "866343"}
编辑
Automator 似乎不喜欢我使用的 property ClassName : class "ClassName"
方法,所以我们必须切换到另一种形式:使用 current application's ClassName's ...
修改后的 Automator AppleScript 看起来像这样(假设文本字符串作为输入传入):
use AppleScript version "2.4"
use framework "Foundation"
on run {input, parameters}
set foundList to my findPattern:"(?<!\d)\d{6}(?!\d)" inString:((item 1 of input) as text)
return foundList
end run
on findPattern:thePattern inString:theString
set theText to current application's NSString's stringWithString:theString
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern ¬
options:0 |error|:(missing value)
set theResult to (theRegEx's matchesInString:theText ¬
options:0 ¬
range:{location:0, |length|:theText's |length|})'s valueForKey:("range")
set outputArray to {}
repeat with thisRange in theResult
copy (theText's substringWithRange:thisRange) as text to end of outputArray
end repeat
return outputArray
end findPattern:inString:
我是 Regex 和 AppleScript 的新手,需要一些支持和指导。
第一个用户输入了一个字符串。它可以是一行或多行中的任何内容。
应在字符串上应用正则表达式以查找只有 6 位数字的数字..不多也不少,并用 space.
分隔它们最终字符串应如下所示:867689, 867617, 866478, 866403, 866343
.
然后这个字符串将被转换成一个列表。 我正在使用这个网站来测试我的正则表达式:https://www.freeformatter.com/regex-tester.html
正好匹配6位数字的正则表达式是:
(?<!\d)\d{6}(?!\d)
我知道为了对 AppleScript 实施正则表达式,我需要使用 Shell 脚本。我也知道我应该使用 sed
但不幸的是我不完全知道如何使用它以及它到底是什么。
通过一些指南和测试,我了解到 sed 不适用于 \d
,我应该改用 [0-9]
,我也应该像这样 \(..\)
转义括号。另外替换 ,
应该像 ,
一样实现。直到这一刻我无法让它工作。
我的测试用户输入如下:
MASTER
ARTIKEL
Artikel
5910020015
867689
PULL1/1
5910020022
867617
PULL1/1
Cappuccino
5910020017
866478
PULL1/1
Braun
5921020017
866403
SHIRT1/2
Kastanie-Multi
5910020016
866343
PULL1/1
和 AppleScript 代码本身:
use scripting additions
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
on list2string(theFoldersList, theDelimiter)
set theBackup to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theString to theFoldersList as string
set AppleScript's text item delimiters to theBackup
return theString
end list2string
on run {input}
display dialog "Please enter your string: " default answer ""
set stringOfNumbers to the text returned of the result
set num to do shell script "sed 's/\(\(?<![0-9]\)[0-9]{6}\(?![0-9]\)\), /' <<< " & quoted form of stringOfNumbers
--(?<!\d)\d{6}(?!\d)
display dialog stringOfNumbers
set stringOfNumbers to current application's NSString's stringWithString:stringOfNumbers
set listOfArtNumbers to (stringOfNumbers's componentsSeparatedByString:", ") as list
display dialog list2string(listOfArtNumbers, ", ")
return input
end run
不幸的是,我在任何地方使用 \
转义字符时都会出错。所以我不得不删除所有 \
但是一旦我 运行 我收到的脚本 "Syntax Error: sed: 1: "s/(?<![0-9])[0-9]{6}(?! ...": unterminated substitute pattern"
我所有的努力都导致了类似的错误。
AppleScript Objective-C 允许我们使用 NSRegularExpression
做正则表达式,从 OS 10.7 (Lion) 开始。以下处理程序 returns 正则表达式搜索结果列表:
use AppleScript version "2.4"
use framework "Foundation"
property NSRegularExpression : class "NSRegularExpression"
property NSString : class "NSString"
on findPattern:thePattern inString:theString
set theText to NSString's stringWithString:theString
set theRegEx to NSRegularExpression's regularExpressionWithPattern:thePattern ¬
options:0 |error|:(missing value)
set theResult to (theRegEx's matchesInString:theText ¬
options:0 ¬
range:{location:0, |length|:theText's |length|})'s valueForKey:("range")
set outputArray to {}
repeat with thisRange in theResult
copy (theText's substringWithRange:thisRange) as text to end of outputArray
end repeat
return outputArray
end findPattern:inString:
请注意,'¬' 符号是一个 line-continuation 符号(在 AppleScript 编辑器中键入 option-return)。我分解了多行以使脚本更具可读性,但这可能 copy/paste 不正确,因此请注意这些应该是单一的、连续的行。
您按如下方式使用此处理程序。请记住,反斜杠在 AppleScript 中是一个特殊字符,因此必须通过在它前面加上另一个反斜杠来转义它:
set foundList to my findPattern:"(?<!\d)\d{6}(?!\d)" inString:"MASTER
ARTIKEL
Artikel
5910020015
867689
PULL1/1
5910020022
867617
PULL1/1
Cappuccino
5910020017
866478
PULL1/1
Braun
5921020017
866403
SHIRT1/2
Kastanie-Multi
5910020016
866343
PULL1/1"
-- Result: {"867689", "867617", "866478", "866403", "866343"}
编辑
Automator 似乎不喜欢我使用的 property ClassName : class "ClassName"
方法,所以我们必须切换到另一种形式:使用 current application's ClassName's ...
修改后的 Automator AppleScript 看起来像这样(假设文本字符串作为输入传入):
use AppleScript version "2.4"
use framework "Foundation"
on run {input, parameters}
set foundList to my findPattern:"(?<!\d)\d{6}(?!\d)" inString:((item 1 of input) as text)
return foundList
end run
on findPattern:thePattern inString:theString
set theText to current application's NSString's stringWithString:theString
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern ¬
options:0 |error|:(missing value)
set theResult to (theRegEx's matchesInString:theText ¬
options:0 ¬
range:{location:0, |length|:theText's |length|})'s valueForKey:("range")
set outputArray to {}
repeat with thisRange in theResult
copy (theText's substringWithRange:thisRange) as text to end of outputArray
end repeat
return outputArray
end findPattern:inString: