在applescript 中是否有类似于regEx 的东西,如果没有,还有什么选择? [英] is there something akin to regEx in applescript, and if not, what's the alternative?

查看:33
本文介绍了在applescript 中是否有类似于regEx 的东西,如果没有,还有什么选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析文件名的前 10 个字符,看看它们是否都是数字.执行此操作的明显方法是 fileName =~ 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 通过执行 shell 脚本".所以:

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 技能不太火爆,所以可能有比将 *good* 附加到匹配 [0-9]{10} 的任何名称然后在开头寻找 *good* 更优雅的方法结果.但基本上,如果文件名是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中转义,你必须转义转义.所以要运行一个有反斜杠的shell脚本,你必须转义它用于像\\这样的shell,然后像\\\\一样在applescript中转义每个反斜杠.这可能很难阅读.

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 调用它来执行(哇哦!).stdout 上的任何结果都会作为结果返回给脚本.

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 中是否有类似于regEx 的东西,如果没有,还有什么选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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