无法弄清楚如何用AppleScript实现REGEX [英] Can't figure out how to implement REGEX with AppleScript
问题描述
我写了一个regex命令来查找并输出字符串中数字行的第一个实例:
I wrote a regex command to find and output the first instance of a line of digits in a string:
find: ^[^\d]*(\d+).*
replace: $1
问题是,为了在AppleScript中实际使用它,我知道的唯一方法是调用Shell脚本并使用sed.我不知道如何以这种方式实际使用我的正则表达式.我已经尝试了好几个小时,没有任何运气.这与我所能接近的差不多,但是它返回字符串中的所有数字,而不是第一组数字:
The problem is that in order to actually utilize this in AppleScript, the only way I know of doing this is with calling a shell script and using sed. I can't figure out how to actually use my regex in this way. I've tried for hours without any luck. This is as close as I can get, but it returns ALL the numbers in a string, rather than the first group of numbers:
set num to do shell script "sed 's/[^0-9]*//g' <<< " & quoted form of input
我真正想要的是一种使用AppleScript与正则表达式一起工作并找到匹配替换项($ 1,$ 2等)的方法.
What I would really like is a way to use AppleScript to just WORK with regex and found match replacement ($1, $2, etc).
推荐答案
请注意, sed
不支持PCRE速记字符类,例如 \ d
,也不支持正则表达式在括号表达式内转义.
Note that sed
does not support PCRE shorthand character classes like \d
, nor does it support regex escapes inside bracket expressions.
此外,由于使用的是 sed
的POSIX BRE风格(不使用 -r
或 -E
选项),因此可以定义捕获组中,您需要 \(... \)
,而不是(...)
.
Also, since you use POSIX BRE flavor of sed
(no -r
or -E
option is used), to define a capturing group, you need \(...\)
, not (...)
.
此外, +
与POSIX BRE模式中的文字 +
符号匹配,您需要对其进行转义,但为了安全起见,只需展开 a +
到 aa *
.
Also, a +
is matching a literal +
symbol in POSIX BRE pattern, you need to escape it, but to play it safe, you can just expand a+
to aa*
.
sed 中的替换反向引用语法为 \
+数字.
使用此POSIX BRE解决方案:
Use this POSIX BRE solution:
sed 's/^[^0-9]*\([0-9][0-9]*\).*/\1/'
,或者,如果使用 -E
或 -r
选项,则为POSIX ERE解决方案:
or, if you use -E
or -r
option, a POSIX ERE solution:
sed -E 's/^[^0-9]*([0-9]+).*/\1/'
详细信息
-
^
-字符串的开头 -
[^ 0-9] *
-0+个除数字以外的字符(也可以使用[[:digit:]] *
) -
\(
-捕获组#1的开始(在替换模式中用\ 1
占位符指代)(在ERE中,(
将启动一个捕获组) -
[0-9] [0-9] *
=[0-9] \ +
(BRE)=[0-9] +
(ERE)-1个以上数字 -
\)
-捕获组的末尾(在POSIX ERE中,)
) -
.*
-该行的其余部分.
^
- start of string[^0-9]*
- 0+ chars other than digits (also, you may use[[:digit:]]*
)\(
- start of a capturing group #1 (referred to with the\1
placeholder from the replacement pattern) (in ERE,(
will start a capturing group)[0-9][0-9]*
=[0-9]\+
(BRE) =[0-9]+
(ERE) - 1+ digits\)
- end of the capturing group (in POSIX ERE,)
).*
- the rest of the line.
这篇关于无法弄清楚如何用AppleScript实现REGEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!