Replacing text in a text object

Get help using and writing Nisus Writer Pro macros.
Post Reply
allenwatson
Posts: 34
Joined: 2005-10-05 15:05:51
Location: Portland, OR
Contact:

Replacing text in a text object

Post by allenwatson »

I've posted a question about a Find, but not with Replace, and somehow I can't seem to get it to work.

I have a text object, $text, containing a paragraph of text where the first word of each sentence is preceded by a one-or-two digit number, e.g.,
"5The next...". I want to remove all the numbers.

I have a one-line macro I've been using to perform this operation on the actual text of a document that looks like this:
Find and Replace @Text< \d+(\w[[:^punct:]])>, " \\1", 'Esa'

That works fine, although I no longer recall why I doubled the brackets. The thing about punctuation is there because (I think) the text may contain citation references in the form of "T-1.VI.4:1" and I do not want to replace anything in the reference. I've been trying to adapt it to work in the process of a longer macro that copies the text, does some stuff with it, and ends by trying to strip out the digits. In any case, this one-liner does the job I want done.

The code I've been trying to adapt for a text object is this:

$text = $text .findAndReplace " \d+(\w[[:^punct:]])", " \1", 'Esa'

What am I doing wrong? It is returning "()".Huh?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Replacing text in a text object

Post by phspaelti »

Starting with the solution, you should remove the "$text = " from your line of code.

The reason is the ".findAndReplace" command will perform on your variable, but the returned result will be an (array of) selection(s). Probably in the case you tested, there weren't any hits? Or maybe the fact that the action also deleted the object over which the selections were made caused an empty array?

Anyhow to back up a bit

Code: Select all

Find and Replace $findExpr, $replaceExpr, $options
Works on the current active document

Code: Select all

$textObject.findAndReplace $findExpr, $replaceExpr, $options
does the same on $textObject.

The first will return a number corresponding to the number of hits, the latter will return a selection object, or an array of selection objects, if you choose the "Find and Replace All" option.

Try

Code: Select all

$result = Find and Replace $findExpr, $replaceExpr, $options

Code: Select all

$result = $textObject.findAndReplace $findExpr, $replaceExpr, $options
and check the difference.

As for your find expression, it's written to match a string of numbers, followed by a single word character, and then a non-punctuation character. The double brackets are necessary, because otherwise the Find engine treats the single bracket as a set, which would mean it would match any of the characters ":", "^", "p", "u", "n", "c", or "t" :lol:
Incidentally note that word characters include numbers, but since search is greedy, the \d+ will be sure to match all the digits and the \w will end up matching only non-digit word characters.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Replacing text in a text object

Post by phspaelti »

As a test case try:

Code: Select all

$text = "Mississippi"
$result = $text.findAndReplace 'i', '*', 'a'
prompt $text, $result
philip
Post Reply