Use the Number of Matches as Part of Replace Pattern

26 views
Skip to first unread message

Christopher Werby

unread,
Aug 21, 2023, 4:32:09 PM8/21/23
to BBEdit Talk
I'm trying to bring Word documents with footnote reference markers into BBEdit in plain text for further editing.  I can surround the special footnote reference markers in Word with strings. I used to have a macro in Word which could grab that reference marker and turn it into text so that it would paste into BBEdit.  But that macro has broken.

Now, in BBEdit, I just have the markers without the actual reference number:

BEGINREF ENDREF

What I would like to do is search using a non-capturing look ahead assertion for the ENDREF (?=ENDREF) and prefix it with the number of matches.  It would look like:

BEGINREF 1ENDREF

BEGINREF 2ENDREF

BEGINREF 3ENDREF ...

Is there a way of doing that?  In UNIX there's a count option flag, but I can't think of how to use that here.

jj

unread,
Aug 22, 2023, 7:47:41 AM8/22/23
to BBEdit Talk
Hi Christopher,

You could use a perl Text Filter.

Place this script in ~/Library/Application Support/BBEdit/Text Filters/add_reference_numbers.pl:

#!/usr/bin/env perl

use v5.14;

use strict;

use warnings;


$/ = undef;


my $i = 1;

print <> =~ s/(?<=BEGINREF\s)\d*(?=ENDREF)/$i++/grimse;


=for test


BEGINREF ENDREF


foo


BEGINREF ENDREF buzz


bar

    BEGINREF ENDREF.


baz



hello   BEGINREF ENDREF world


BEGINREF

ENDREF


=cut


And call it from menu Text > Apply Text Filter > add_reference_numbers.

HTH,

Jean Jourdain
Reply all
Reply to author
Forward
0 new messages