Copy link to clipboard
Copied
I'm having a problem using capture replace groups in my Find/Change script. For example, I'm fixing missing hyphenation in phrases. I find "aim off points" using \b([Aa]im[ ]?off[ ]?point)([s]?) and replace it with aim-off point$2, which should give me "aim-off points" as my replacement. My find/replace works perfectly using FMs Find/Change panel (see below). When I try it in my script I get this result: aim off pointaim-off points$2. The $2 capture group searches for the plural of "points." In my script tester I am using this to mimic the FM Find/Change panel:
FindReplace ("\\b([Aa]im[ ]?off[ ]?point)([s]?)", "aim-off point$2"); I've searched the blogs, reviewed K. Daube's Find/Change PDF, and tried various syntax combinations, without luck. Any help much appreciated.
Copy link to clipboard
Copied
Fightengator, I would use another RegEx:
Search: \b(A|a)im off point
Replace: $1im-off point
this works for me correctly both in FM Find/Change and in my own script FMfindRepl.
IT depends on your general case how the RegEx should look then.
Copy link to clipboard
Copied
Thanks K. Daube, I'll give that a try. That's a good idea for using the first capture group to capture the case. However, my real goal is to use a second capture group to capture the capture the word suffixes; i.e., "fine-tune, fine-tuning, fine-tunes, or fine-tuned and add it to the replacement. When I use:
Search: fine-tun([eing]{1,3}) //matches fine-tune and fine-tuning
Replace: fine-tun$1
I get "fine-tun$1" as the replacement. I'm sure I'm doing something wrong with my search / replace strings, but haven't succeeded yet.
Copy link to clipboard
Copied
Was traveling all last week, so just now getting back to this issue. I figured out why the replace script isn't appending the $2 capture group correctly. I'm not using an actual FM ChangeToText function. Instead I'd built a simple routine that inserted a replacement string at the end of the text that was found using doc.AddText. Accordinly, it inserts my replacement text literally; "fin-tun$2". I need a script to insert the replacement text using a function that mimics the FM ChangeToText dialogue. If I'm unable to figure it out on my own, will start a new post to the forum.
Copy link to clipboard
Copied
Back on my script project after being tied up for the past three weeks. I ran across a 2018 post from Rick that explained my problem. I was simply taking the ReplaceString from my table and inserting it after the found text, instead of doing this...
if (regex.test (text) === true) {
text = text.replace (regex, "$1.$2");
alert (text);
}
Thanks again for taking time to reply. I should be able to get this working now.