For a human generated solution... 

Since simple grep search and replace can't easily handle generating 
variable length strings whose length is determined by the length of a found 
text group, something like a simple perl text filter is an easier way to 
handle it.

A simple perl text filter like:

#!/usr/bin/perl -w
use strict;

# set up the underline character for heading levels
my $ulCharL1 = "=";
my $ulCharL2 = "-";

#for each line check for and reformat level 1 and level 2 headings
while (<>) {
chomp;
if ($_ =~ /^## (.+)$/){
print $1, "\n", $ulCharL2 x length($1), "\n";
} elsif ($_ =~ /^# (.+)$/){
print $1, "\n", $ulCharL1 x length($1), "\n";
} else {
print $_, "\n";
}
}

will quickly do the job.

I saved it as "underline_headings.pl in in BBEdit's Text Filters support 
folder. (If desired, you can assign a keyboard short cut for in the Text 
Filters pallet.)

Then you can apply the text filter to either the frontmost whole file/text 
window or just to selected text in the frontmost file/text window.

In the match patterns for heading lines, I'm assuming heading lines will 
always start at the beginning of a line with # or ## followed by just one 
space character followed by the rest of the heading line text to the end of 
that line. If those assumptions don't hold with the level headings forms 
found in your real world text, you'll need to modify the match grep 
expressions.

Also, if you have additional levels of heading besides the two, to handle 
those just declare the underline character for that heading level and add 
another elsif clause to find/match that heading level and print the 
reformatted heading text with the underline text line.

On Monday, April 22, 2024 at 10:31:18 AM UTC-7 Doug Pinkerton wrote:

> I would like to create a text factory to change each instance of 
>
> # This is a level 1 heading
> to
> This is a level 1 heading
> =========================
>
> And I would like to change each instance of
>
> ## This is a level 2 heading
> to 
> This is a level 2 heading
> -------------------------
>
> My grep skills are rudimentary. I’m using this.
>
> Find: ^## ([A-Za-z: ,]+)\r
> Replace: \1\r----------
>
> It works as desired, almost. But instead of ten hardcoded dashes, I would 
> like for the number of dashes to equal the number of characters in the 
> previous line (once the initial hashmarks are removed). I’m in over my 
> head. Can someone help?
>
> Thanks.
>

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or believe that the application isn't working correctly, please email 
"[email protected]" rather than posting here. Follow @bbedit on Mastodon: 
<https://mastodon.social/@bbedit>
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/c7a1fa4f-5cc2-40ff-81b5-5ce954f08f28n%40googlegroups.com.

Reply via email to