问题
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
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
What I would really like is a way to use AppleScript to just WORK with regex and found match replacement ($1, $2, etc).
回答1:
Note that sed
does not support PCRE shorthand character classes like \d
, nor does it support regex escapes inside bracket expressions.
Also, since you use POSIX BRE flavor of sed
(no -r
or -E
option is used), to define a capturing group, you need \(...\)
, not (...)
.
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*
.
Replacement backreference syntax in sed
is \
+ number.
Use this POSIX BRE solution:
sed 's/^[^0-9]*\([0-9][0-9]*\).*/\1/'
or, if you use -E
or -r
option, a POSIX ERE solution:
sed -E 's/^[^0-9]*([0-9]+).*/\1/'
Details
^
- 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.
回答2:
Although you have your solution, I thought it might be useful to see another method of implementing regular expression matching and replacement using AppleScript (actually, AppleScript-ObjC):
use framework "Foundation"
use scripting additions
--------------------------------------------------------------------------------
set regex to "(^[^\\d]*)(\\d+)(.*)"
set input to "There are 250 billion stars in the galaxy, " & ¬
"and 200 billion galaxies in the observable universe."
re_match from the input against regex ¬
given replacement:"$1two-hundred-and-fifty$3"
--------------------------------------------------------------------------------
on re_match against pattern from str given replacement:fmt
set regex to current application's NSRegularExpression's ¬
regularExpressionWithPattern:pattern ¬
options:(current application's ¬
NSRegularExpressionCaseInsensitive) ¬
|error|:(missing value)
(regex's stringByReplacingMatchesInString:str ¬
options:0 range:{0, length of str} ¬
withTemplate:fmt) ¬
as text
end re_match
Result:
"There are two-hundred-and-fifty billion stars in the galaxy, and 200 billion galaxies in the observable universe."
来源:https://stackoverflow.com/questions/50865437/cant-figure-out-how-to-implement-regex-with-applescript