Hello Allen,
This being Nisus you will be doing this with Find/Replace no matter what.
To start with the solution, this will get you what you want:
Basically, you can do Find/Replace on text objects in more or less the same way that you would do them in the app itself. So if we compare the following two things:
Code: Select all
Find "\d+", "E"
$mytext.find "\d+", "E"
they will both find the first string of digits in the relevant text object. The menu command "Find" will actually create a selection in the GUI, while the latter will return a 'virtual' selection, in the form of a TextSelection object. If you catch the result(s) as follows:
Code: Select all
$result = Find "\d+", "E"
$result = $mytext.find "\d+", "E"
The former will still create the selection in the GUI, and $result will return the number of hits. The latter will return a TextSelection object—or in the case of
.findAll an array of TextSelection objects.
So one way to extract a number from a text object would be:
Code: Select all
$mytextselection = $mytext.find "\d+", "E"
$refnum = $mytextselection.substring
To understand why you need to do this this way, you will need to check on how TextSelections work (which I wrote about in other threads).
This method can hit its limit when the pattern you are trying to find is more complicated. But ".find" has another method that turns the found bits into variables. You could do the same like this:
Code: Select all
$mytextselection = $mytext.find "\d+", "E¢"
$refnum = $0
In this case using the "¢" option—only possible with single hit .find,
not with .findAll—returns the found bits as number variables: $0 for the whole thing, $1 for the first match, $2 for the second, etc.
The "¢" option also allows you to name the variables yourself. For this you need to use (?<variable_name> … ) match parentheses. This is only possible using PowerFind Pro. That leads to the above mentioned solution.