Yet *another* delimiters question (replace only if)

I’ve got a workable solution without a dependency, but I could use some guidance on one thing in specific. Why does #2 below not run?

1. This Works for the combination “ti”:

set findTI to "\\([a-zA-Z]\\)\\([0-9+%-]\\)\\([a-zA-Z]\\)"
set replaceTI to "\\1ti\\3"
set theWords to "Propriocep4ve, Posi+on, Contraindica%ons, a`achments, sta-cally, Lingual$nerve, ves6bule, supraglo.c, respiratory)center" --restric-ve"
set theshell to do shell script "echo " & quoted form of theWords & " | sed s/" & quoted form of findTI & "/" & quoted form of replaceTI & "/g"

2. This DOES NOT WORK for combinations “ti” and space:

set findTI to "\\([a-zA-Z]\\)\\([0-9+%-]\\)\\([a-zA-Z]\\)"
set replaceTI to "\\1ti\\3"
set findSpace to "\\([a-zA-Z]\\)\\([$)]\\)\\([a-zA-Z]\\)"
set replaceSpace to "\\1 \\3"
set theWords to "Propriocep4ve, Posi+on, Contraindica%ons, a`achments, sta-cally, Lingual$nerve, ves6bule, supraglo.c, respiratory)center" --restric-ve"
set theshell to do shell script "echo " & quoted form of theWords & " | sed s/" & quoted form of findTI & "/" & quoted form of replaceTI & "/g" & ";s/" & quoted form of findSpace & "/" & quoted form of replaceSpace & "/g"

3. This works for “4”, “ti”, “+”, “.”, “`”, “-”, “%”, space:

set theWords to "Propriocep4ve, Posi+on, Contraindica%ons, a`achments, sta-cally, Lingual$nerve, ves6bule, supraglo.c, respiratory)center" --restric-ve"
set theshell to do shell script "echo " & quoted form of theWords & " | sed 's/\\([a-zA-Z]\\)\\([0-9+%-]\\)\\([a-zA-Z]\\)/\\1ti\\3/g;s/\\([a-zA-Z]\\)\\([$)]\\)\\([a-zA-Z]\\)/\\1 \\3/g;s/\\([a-zA-Z]\\)\\([.`]\\)\\([a-zA-Z]\\)/\\1tt\\3/g'"

Hey There,

You really ought to be using the extended regex switch ‘-E’ in sed. (Amongst other things quoting parentheses is unnecessary when you do this).
Improperly quoted sed in your shell script.

This version works, although not all the replacements are working yet (but I’m not going to fiddle with those).

set findTI to "([a-zA-Z])([0-9+%-])([a-zA-Z])"
set replaceTI to "\\1ti\\3"
set findSpace to "([a-zA-Z])([$)])([a-zA-Z])"
set replaceSpace to "\\1 \\3"
set theWords to "Propriocep4ve, Posi+on, Contraindica%ons, a`achments, sta-cally, Lingual$nerve, ves6bule, supraglo.c, respiratory)center" --restric-ve"
set _cmd to "echo " & quoted form of theWords & " \\
| sed -E 's!" & findTI & "!" & replaceTI & "!g" & "; s!" & findSpace & "!" & replaceSpace & "!g'"

do shell script _cmd

It’s always a good idea to write your shell script stuff in the shell and get it working before fooling around with the quoting issues of running a do shell script statement in AppleScript. I generally use a BBEdit worksheet for this, but you can also use the freeware editor Eddie or the actual shell of course.

Personally I’d use the Satimage.osax OR a Mavericks Library for the regex rather than messing with the shell.

If you want to send me one of these PDFs I have a shell tool that (so far) is better than anything else I’ve tried for extracting text from them. It would only take a couple of minutes to test. listmeister@thestoneforge.com