Regex 在AppleScript中实现正则表达式以精确匹配6个数字

Regex 在AppleScript中实现正则表达式以精确匹配6个数字,regex,applescript,Regex,Applescript,我不熟悉Regex和AppleScript,我需要一些支持和指导 第一个用户输入一个字符串。它可以是一行或多行中的任何内容 应在字符串上应用正则表达式,以便查找只有6位的数字。不多或少,并用空格分隔 最后一个字符串应该如下所示:867689、867617、866478、866403、866343 然后,该字符串将转换为列表。 我正在使用此网站测试我的正则表达式: 与6位数字完全匹配的正则表达式是: (? 我知道为了将Regex实现为AppleScript,我需要使用Shell脚本。我也知道我应该

我不熟悉Regex和AppleScript,我需要一些支持和指导

第一个用户输入一个字符串。它可以是一行或多行中的任何内容

应在字符串上应用正则表达式,以便查找只有6位的数字。不多或少,并用空格分隔

最后一个字符串应该如下所示:
867689、867617、866478、866403、866343

然后,该字符串将转换为列表。 我正在使用此网站测试我的正则表达式:

与6位数字完全匹配的正则表达式是:
(?
我知道为了将Regex实现为AppleScript,我需要使用Shell脚本。我也知道我应该使用
sed
,但不幸的是,我不完全知道如何使用它以及它到底是什么

通过一些指导和测试,我了解到sed不能与
\d
一起工作,我应该使用
[0-9]
,我也应该避免像这样的括号
\(…\)
。还应该替换
$1,
应该像
\1,
一样实现。直到现在,我还是无法让它工作

我的测试用户输入如下:

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]\)\)\1, /' <<< " & 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
使用脚本添加
使用AppleScript版本“2.4”--约塞米蒂(10.10)或更高版本
使用框架“基础”
在列表2字符串上(文件夹列表、删除器)
将备份设置为AppleScript的文本项分隔符
将AppleScript的文本项分隔符设置为Delimiter
将字符串设置为字符串形式的文件夹列表
将AppleScript的文本项分隔符设置为备份
返回字符串
结束list2string
运行时{input}
显示对话框“请输入字符串:”默认答案“”
将StringOfNumber设置为结果返回的文本
将num设置为执行shell脚本“sed的/\(\(?)?

不幸的是,每当我使用
\
转义字符时,都会出现一个错误。因此,我必须删除所有
\
,但一旦我运行脚本,就会收到
“语法错误:sed:1:”s/(?我所有的努力都导致了类似的错误。

AppleScript Objective-C允许我们从OS 10.7(Lion)开始,使用
NSRegularExpression
执行正则表达式。以下处理程序以列表形式返回正则表达式搜索的结果:

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:
请注意,“,”符号是一个行延续符号(在AppleScript编辑器中键入option return)。我已将行拆分,以使脚本更可读,但这可能无法正确复制/粘贴,因此请注意,这些行应为单行连续行

您可以按如下方式使用此处理程序。请记住,反斜杠是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"}

如果您要查找的是6位数字,这也是字符串的开始和结束…那么您可以只使用
^[0-9]{6}$
,对吗?“我知道为了实现Regex,我需要使用Shell脚本。我还知道我应该使用sed“。您是如何意识到这两大智慧的?AppleScript也使用反斜杠作为字符串文本中的转义字符,因此必须转义两次:
”(?对此有一个AppleScriptObjC解决方案,但我不确定您是否感兴趣。它至少需要OS 10.7。请告诉我,我会发布。@foo我尝试过两次转义,但结果仍然是一样的。由于sintax错误,无法正确执行。文章底部是更新/编辑版本。有任何建议我在哪里aken?根据您的指南,我已经成功地使用mac脚本编辑器运行了它,没有任何问题。非常感谢:)!但是由于某些原因,我无法使用Automator运行它。我得到的错误是语法错误无效密钥形式。以及代码类“NSRegularExpression”"已突出显示。由于此代码将是更长脚本的一部分,我真的需要在Automator中使用,以便只需单击一下即可执行。另一个选项是,我只需使我已经制作的脚本编辑器文件以某种方式可执行,并使用Automator启动它。不知道如何执行。作者OutOfTouch@OutOfTouch:服装Thly Automator不喜欢我用于创建类引用的属性方法。因此,请删除属性引用并使用其他引用形式:当前应用程序的NSRegularExpression。我将更新我的答案–由Ted编写Wrigley@TedWrigley我已尝试在Automator中运行更新版本,但不幸的是,我收到了一个错误我不知道是什么原因造成的。请你检查一下。我把细节放在我原来的后期编辑部分。谢谢!@OutOfTouch:如果你要使用Automator,请使用Automator。回到我编写的原始脚本,然后在它前面放入
请求文本
Automator操作。Automator操作将从用户,并将其传递给
运行AppleScript
操作。或者,如果您确实需要使用AppleScript的
显示对话框
,请添加其他
运行AppleScript
以运行DD命令,然后将结果传递给带有我提供的脚本的AS操作。请考虑“模块化”;这就是自动机的工作方式。不要尝试在n任何一个操作。@OutOfTouch:我使用我编写的脚本测试了它(未修改)前面有一个
Ask-For-Text
Automator操作,它按预期工作,没有错误。您是否仍然收到与以前注意到的相同错误?请确保已删除
use-scripting additions
行,我知道这行代码与Automator不兼容。
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: