Can't figure out how to implement REGEX with AppleScript

半城伤御伤魂 提交于 2021-02-08 04:00:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!