2

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).

0

2 Answers 2

2

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.
6
  • Can you break this expression down for me? Why is the found expression using [0-9] twice? I removed that extra [0-9] and it worked the same way... Jun 14, 2018 at 21:07
  • @PatrickHennessey I explained that bit already, a+ = aa*. + must be escaped in BRE POSIX pattern to denote a quantifier, but in some environments, + just does not work. I will add a POSIX ERE alternative. Jun 14, 2018 at 21:09
  • 1
    This did the trick! While I'm here, is there a more concise way to achieve this using egrep? I feel like sed might not be the best command to use in order to get that "regex feel." I'd rather be able to use regex with PCRE. Jun 14, 2018 at 21:47
  • @PatrickHennessey I think this is the best so far. egrep BSD version is good to use, but I do not know if you can use it. grep -Eo '[0-9]+'. Jun 14, 2018 at 21:59
  • Is there a way to use grep to search a string input like we do with sed? Jun 14, 2018 at 22:01
1

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."

3
  • That's disappointingly complicated. Isn't there a shell function that takes regex expressions directly and applies them to strings?? Jun 15, 2018 at 21:58
  • Regular Expressions aren't the only form of pattern matching. Bash has powerful, built-in pattern matching, which is similar in syntax to regular expressions. So you can take a bash pattern and directly apply those to strings.
    – CJK
    Jun 15, 2018 at 22:21
  • Another method is to employ an application's AppleScript functions. For instance, the text editor BBEdit is free, AppleScriptable and provides TONS of text manipulation functions that you can call from within your scripts, including grep (regex) replacement, duplicate line matching, sorting, etc.
    – CJK
    Jun 15, 2018 at 22:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.