有没有在AppleScript的一个类似于正则表达式,如果没有,有什么替代方案? [英] is there something akin to regEx in applescript, and if not, what's the alternative?

查看:943
本文介绍了有没有在AppleScript的一个类似于正则表达式,如果没有,有什么替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析的文件名称的第一个10个字符,以查看它们是否所有数字。最显而易见的方法做,这是文件名=〜M / ^ \\ d {10} /但我不会在AppleScript的参考看到任何东西regExy,所以,我很好奇,我不得不这样做验证什么其他选择。

I need to parse the first 10 chars of a file name to see if they are all digits. The obvious way to do this is fileName =~ m/^\d{10}/ but I'm not seeing anything regExy in the applescript reference, so, I'm curious what other options I have to do this validation.

推荐答案

不要绝望,因为OSX您也可以访问的 sed的通过和grep做外壳脚本。所以:

Don't despair, since OSX you can also access sed and grep through "do shell script". So:

set thecommandstring to "echo \"" & filename & "\"|sed \"s/[0-9]\\{10\\}/*good*(&)/\"" as string
set sedResult to do shell script thecommandstring
set isgood to sedResult starts with "*good*"

我的sed的技能是不是太崩溃热,所以有可能会比追加一个更优雅的方式* *好,以匹配任何名称[0-9] {10},然后寻找* *良好的开始处结果。但基本上,如果文件名是1234567890dfoo.mov这将运行命令:

My sed skills aren't too crash hot, so there might be a more elegant way than appending *good* to any name that matches [0-9]{10} and then looking for *good* at the start of the result. But basically, if filename is "1234567890dfoo.mov" this will run the command:

echo "1234567890foo.mov"|sed "s/[0-9]\{10\}/*good*(&)/"

请注意的转义引号\\,并躲过了AppleScript的反斜杠\\\\。如果你在你逃跑的逃跑外壳逃逸的事情。因此,要运行,有一个反斜杠它,你必须逃脱的shell脚本它像\\\\壳,然后逃离像\\\\\\\\ AppleScript的每一个反斜杠。这可以得到pretty难以阅读。

Note the escaped quotes \" and escaped backslash \\ in the applescript. If you're escaping things in the shell you have to escape the escapes. So to run a shell script that has a backslash in it you have to escape it for the shell like \\ and then escape each backslash in applescript like \\\\. This can get pretty hard to read.

所以,任何你可以在命令行可以通过从AppleScript的调用它做做(呜​​呼!)。在标准输出上的任何查询结果返回给脚本的结果。

So anything you can do on the command line you can do by calling it from applescript (woohoo!). Any results on stdout get returned to the script as the result.

这篇关于有没有在AppleScript的一个类似于正则表达式,如果没有,有什么替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆