Look-behind in macro

Get help using and writing Nisus Writer Pro macros.
Post Reply
branner
Posts: 2
Joined: 2017-08-06 05:33:06

Look-behind in macro

Post by branner » 2017-08-06 05:51:55

I have a two-level Number List. The level-1 items are numbered 1,2,3,4 and the level-2 sub-items are numbered a,b,c,d. There is a sample file attached.

I want to automatically add a sub-item "b" after each sub-item "a'. I can do this manually with Powerfind Pro, finding

Code: Select all

(?<=a\.\t)(.+?)$
and replacing it with

Code: Select all

\1\nNEW LEVEL-2 "b" here
The look-behind expression is able to recognize level-2 sub-items that begin with "a".

But when I construct a macro:

Code: Select all

Find and Replace '(?<=a\.\t)(.+?)$', '\1\nNEW LEVEL-2 "b" here', 'EaW'
it does not produce the expected results. Am I doing something wrong?
Attachments
nisus_writer_question_abt_look-behind_in_macros_20170806_sample_file.rtf
(19.16 KiB) Downloaded 86 times

Þorvarður
Posts: 251
Joined: 2012-12-19 05:02:52

Re: Look-behind in macro

Post by Þorvarður » 2017-08-07 09:19:05

Hi Branner,
You say
it does not produce the expected results
Your Find and Replace code adds indeed a sub-item "b" after each sub-item "a", so I'm not sure what your problem is.
What are the "expected results" you're expecting?

Is it perhaps the additional text
NEW LEVEL-2 "b" here
which you don't want? If yes, then don't include it in the replace part. It should then look like this:
Find and Replace '(?<=a\.\t)(.+?)$', '\1\n', 'EaW'
or
Find and Replace @Text{(?<=a\.\t)(.+?)$$}, @Text<\1\n>, 'Ea'

User avatar
phspaelti
Posts: 916
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Look-behind in macro

Post by phspaelti » 2017-08-07 10:31:05

I agree with Þorvarður. The macro gives the same result as doing the Find/Replace from the Find dialog for me. So I'm not sure what your think is going wrong.

A different problem which I really think Martin should have a look at, is that after this Find/Replace the selections are shifted. The first one is okay. The second starts at the dot of the auto-number and the third reaches even further back.
philip

branner
Posts: 2
Joined: 2017-08-06 05:33:06

Re: Look-behind in macro

Post by branner » 2017-08-07 12:58:36

Indeed, it now works as expected. I don't know what went wrong — sorry for the trouble!

User avatar
martin
Official Nisus Person
Posts: 4288
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Look-behind in macro

Post by martin » 2017-08-09 10:11:52

phspaelti wrote:A different problem which I really think Martin should have a look at, is that after this Find/Replace the selections are shifted. The first one is okay. The second starts at the dot of the auto-number and the third reaches even further back.
Thank you for calling this out Philip. You're right that the selection is off after the replacement for some matches. I'll take a look, thanks!

Post Reply