如果字符串不包含和REGEX

时间:2019-04-11 21:40:57

标签: applescript

我正在寻找一种在applescript中编写以下javascript代码的方法:如果条件为false,那么我想做些事情。

var regEx = /\d{5}/g;
var str = 'This string contains 12345';

if (!regEx.test(str)){
    do something
}

下面是我开始的小程序,但是它不起作用。

set str to 'This string contains 12345'
set regEx to <NOT SURE HOW APPLESCRIPT HANDLES THIS>

if string does contains "12345" then
 do something
end if 

使用Javascript! =不。 applescript中的等效项是什么?以及如何处理RegEx?

我的总体目标是查明所选的查找程序窗口在文件夹名称中是否不包含任何5位数字组合。

2 个答案:

答案 0 :(得分:3)

tl; dr 对于>= OSX 10.8的任何版本的macOS,您需要替换grep的{​​{1}}选项(如所示)在下面的“解决方案” 部分中),并带有-P选项-如底部的“不同的-E实用程序” 部分中所述发布。


如注释中正确指出的那样...

  

Vanilla AppleScript无法处理正则表达式。 -vadian

所以您需要

  

掏出一些不知道正则表达式的东西-red_menace


解决方案:

要以类似于JavaScript的test()方法的方式满足对Vanilla AppleScript的需求,请考虑如下使用自定义AppleScript子例程:

子例程:

grep

用法:

on regExpTest(str, re)      
  set statusCode to do shell script "grep -q -P " & quoted form of re & ¬
    " <<<" & quoted form of str & " 2>/dev/null; echo $?"

  if statusCode is equal to "0" then
    return true
  else
    return false
  end if
end regExpTest

运行上述脚本将显示带有给定消息的对话框,因为正则表达式与指定字符串之间存在匹配。

注意::AppleScript字符串使用反斜杠作为转义字符,因此您会注意到set regExp to "\\d{5}" set str to "This string contains 12345" if regExpTest(str, regExp) then display dialog "It DOES match so let's do something" end if 元字符已通过附加反斜杠(即{{ 1}}

不等式运算符:

  

在Javascript \d中没有。 applescript中的等效项是什么?以及如何处理RegEx?

AppleScript的inequality operatorsJavaScripts inequality operator\\d)类似:

!=

因此,鉴于您的JavaScript !=语句:

≠
is not
isn't
isn't equal [to]
is not equal [to]
doesn't equal
does not equal

我们可以使用以下代码实现相同的逻辑(再次使用上述自定义if子例程):

if (!regEx.test(str)){
  // do something
}

注意 regExpTest值仅包含四个连续的数字,即set regExp to "\\d{5}" set str to "This string contains 1234" if regExpTest(str, regExp) ≠ true then display dialog "It DOES NOT match so let's do something" end if

这一次运行上述脚本将显示带有给定消息的对话框,因为正则表达式与指定字符串之间没有匹配。

可以对上述AppleScript str语句进行多种更改以实现相同的所需逻辑。例如;

1234
if

等...


if regExpTest(str, regExp) is not equal to true then ... end if 子例程说明:

前面提到的if regExpTest(str, regExp) = false then ... end if AppleScript子例程实际上是利用do shell script命令来运行以下代码,这些代码将直接通过macOS regExpTest应用程序运行。例如,在您的regExpTest应用程序中,运行以下两个命令:

Terminal

打印:

  

Terminal

grep -q -P "\d{5}" <<<"This string contains 12345" 2>/dev/null; echo $?

打印:

  

0


编辑:不同的grep -q -P "\d{5}" <<<"This string contains 1234" 2>/dev/null; echo $? 实用程序:

user3439894的注释中所述,似乎Mac上安装的grep实用程序的某些版本不支持1选项,该选项确保将RegExp模式解释为Perl。正则表达式。我之所以选择使用Perl正则表达式,是因为它们与JavaScript中使用的正则表达式更加紧密一致。

但是,如果您通过命令行运行grep,并且发现您的-P实用程序不提供man grep选项,请在{{1 }}子例程:

grep

对此:

-P

注意regExpTest选项已更改为set statusCode to do shell script "grep -q -P " & quoted form of re & ¬ " <<<" & quoted form of str & " 2>/dev/null; echo $?" ,因此该模式现在被解释为扩展的正则表达式(ERE)。

简写set statusCode to do shell script "grep -q -E " & quoted form of re & ¬ " <<<" & quoted form of str & " 2>/dev/null; echo $?"

您可能还会发现您需要从以下位置更改正则表达式模式的分配:

-P

-E

这次,速记元字符\d(与数字匹配使用)已被替换为等效的字符类set regExp to "\\d{5}"

答案 1 :(得分:0)

正如其他人所说,您可以通过AppleScript-ObjC桥使用Foundation框架的NSRegularExpression

也就是说,Objective-C API虽然功能强大,但并非完全对AppleScripter友好,因此几年前,我将“standard libraries”组合在一起,将许多通用功能包装为漂亮的本地AppleScript命令。

例如这是使用Text库的search text命令的与JavaScript最接近的等效项:

use script "Text"

set str to "This string contains 12345"

set foundMatches to search text str for "\\d{5}" using pattern matching

if foundMatches is not {} then
    -- do something
end if

无法引起太多兴趣,所以我不再进行开发或支持。但是它们是免费和开放的(就我而言,这是公共领域的),并且在当前版本的macOS AFAIK上仍然可以正常工作,所以请自助。

相关问题