Gen*_* S. 29 regex applescript
我需要解析文件名的前10个字符,看看它们是否都是数字.显而易见的方法是fileName = ~m/^\d {10} /但是我没有在applescript参考中看到任何regExy,所以,我很好奇我还有哪些其他选项可以做这个验证.
sti*_*tib 24
不要绝望,因为OSX你也可以通过"do shell script" 访问sed和grep.所以:
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*"
Run Code Online (Sandbox Code Playgroud)
我的sed技能不是太热,所以可能有一种更优雅的方式,就是将*good*附加到匹配[0-9] {10}的任何名称,然后在结果的开头寻找*good*.但基本上,如果filename是"1234567890dfoo.mov",这将运行命令:
echo "1234567890foo.mov"|sed "s/[0-9]\{10\}/*good*(&)/"
Run Code Online (Sandbox Code Playgroud)
注意转载的引号\"并且在applescript中转义反斜杠\\.如果你要逃避shell中的东西,你必须逃避转义.所以要运行一个反斜杠的shell脚本,你必须为它逃脱它像\\那样的shell,然后像\\\\那样转换AppleScript中的每个反斜杠.这可能很难阅读.
所以你可以在命令行上做任何事情,你可以通过从AppleScript调用它来做(woohoo!).stdout上的任何结果都会返回到脚本中作为结果.
mkl*_*nt0 18
有一种更简单的方法可以使用shell(适用于bash 3.2+)进行正则表达式匹配:
set isMatch to "0" = (do shell script ¬
"[[ " & quoted form of fileName & " =~ ^[[:digit:]]{10} ]]; printf $?")
Run Code Online (Sandbox Code Playgroud)
注意:
[[ ... ]]正则表达式匹配运算符使用现代bash测试表达式=~; 不引用正确的操作数(或至少特殊的正则表达式字符.)是必须在bash 3.2+上,除非你在前置shopt -s compat31;do shell script语句执行测试并通过附加命令返回其exit命令(感谢@LauriRanta); "0"表示成功.=~操作者不支持快捷键的字符类如\d和断言,如\b(如OS X 10.9.4的真实-这是不太可能很快改变).shopt -s nocasematch;export LANG='" & user locale of (system info) & ".UTF-8';.${BASH_REMATCH[@]}数组变量访问捕获的字符串.\-escape双引号和反斜杠.这是另一种选择egrep:
set isMatch to "0" = (do shell script ¬
"egrep -q '^\\d{10}' <<<" & quoted form of filename & "; printf $?")
Run Code Online (Sandbox Code Playgroud)
虽然这可能表现更差,但它有两个优点:
\d和断言\begrep,您可以更轻松地使匹配大小写不敏感-i:[[ ... =~ ... ]]如果需要,请使用该方法.最后,这里是包含两种方法的实用程序函数(语法突出显示已关闭,但它们确实有效):
# SYNOPIS
# doesMatch(text, regexString) -> Boolean
# DESCRIPTION
# Matches string s against regular expression (string) regex using bash's extended regular expression language *including*
# support for shortcut classes such as `\d`, and assertions such as `\b`, and *returns a Boolean* to indicate if
# there is a match or not.
# - AppleScript's case sensitivity setting is respected; i.e., matching is case-INsensitive by default, unless inside
# a 'considering case' block.
# - The current user's locale is respected.
# EXAMPLE
# my doesMatch("127.0.0.1", "^(\\d{1,3}\\.){3}\\d{1,3}$") # -> true
on doesMatch(s, regex)
local ignoreCase, extraGrepOption
set ignoreCase to "a" is "A"
if ignoreCase then
set extraGrepOption to "i"
else
set extraGrepOption to ""
end if
# Note: So that classes such as \w work with different locales, we need to set the shell's locale explicitly to the current user's.
# Rather than let the shell command fail we return the exit code and test for "0" to avoid having to deal with exception handling in AppleScript.
tell me to return "0" = (do shell script "export LANG='" & user locale of (system info) & ".UTF-8'; egrep -q" & extraGrepOption & " " & quoted form of regex & " <<< " & quoted form of s & "; printf $?")
end doesMatch
# SYNOPSIS
# getMatch(text, regexString) -> { overallMatch[, captureGroup1Match ...] } or {}
# DESCRIPTION
# Matches string s against regular expression (string) regex using bash's extended regular expression language and
# *returns the matching string and substrings matching capture groups, if any.*
#
# - AppleScript's case sensitivity setting is respected; i.e., matching is case-INsensitive by default, unless this subroutine is called inside
# a 'considering case' block.
# - The current user's locale is respected.
#
# IMPORTANT:
#
# Unlike doesMatch(), this subroutine does NOT support shortcut character classes such as \d.
# Instead, use one of the following POSIX classes (see `man re_format`):
# [[:alpha:]] [[:word:]] [[:lower:]] [[:upper:]] [[:ascii:]]
# [[:alnum:]] [[:digit:]] [[:xdigit:]]
# [[:blank:]] [[:space:]] [[:punct:]] [[:cntrl:]]
# [[:graph:]] [[:print:]]
#
# Also, `\b`, '\B', '\<', and '\>' are not supported; you can use `[[:<:]]` for '\<' and `[[:>:]]` for `\>`
#
# Always returns a *list*:
# - an empty list, if no match is found
# - otherwise, the first list element contains the matching string
# - if regex contains capture groups, additional elements return the strings captured by the capture groups; note that *named* capture groups are NOT supported.
# EXAMPLE
# my getMatch("127.0.0.1", "^([[:digit:]]{1,3})\\.([[:digit:]]{1,3})\\.([[:digit:]]{1,3})\\.([[:digit:]]{1,3})$") # -> { "127.0.0.1", "127", "0", "0", "1" }
on getMatch(s, regex)
local ignoreCase, extraCommand
set ignoreCase to "a" is "A"
if ignoreCase then
set extraCommand to "shopt -s nocasematch; "
else
set extraCommand to ""
end if
# Note:
# So that classes such as [[:alpha:]] work with different locales, we need to set the shell's locale explicitly to the current user's.
# Since `quoted form of` encloses its argument in single quotes, we must set compatibility option `shopt -s compat31` for the =~ operator to work.
# Rather than let the shell command fail we return '' in case of non-match to avoid having to deal with exception handling in AppleScript.
tell me to do shell script "export LANG='" & user locale of (system info) & ".UTF-8'; shopt -s compat31; " & extraCommand & "[[ " & quoted form of s & " =~ " & quoted form of regex & " ]] && printf '%s\\n' \"${BASH_REMATCH[@]}\" || printf ''"
return paragraphs of result
end getMatch
Run Code Online (Sandbox Code Playgroud)
Dov*_*Dov 11
我最近需要在脚本中使用正则表达式,并希望找到一个脚本添加来处理它,因此更容易阅读正在发生的事情.我找到了Satimage.osax,它允许你使用如下语法:
find text "n(.*)" in "to be or not to be" with regexp
Run Code Online (Sandbox Code Playgroud)
唯一的缺点是(截至2010年8月11日)它是一个32位的添加,因此当它从64位进程调用时会抛出错误.这位于Snow Leopard的Mail规则中,因为我必须以32位模式运行Mail.但是,从一个独立的脚本调用,我没有任何保留 - 它真的很棒,并且允许你选择你想要的任何正则表达式语法,并使用反向引用.
2011年5月28日更新
感谢Mitchell Model在下面的评论指出他们已将其更新为64位,因此不再需要预订 - 它可以完成我需要的一切.
| 归档时间: |
|
| 查看次数: |
17723 次 |
| 最近记录: |