Store portion of regex match in variable or regex find and replace

Hi. First post here.

I have quite a few markdown files that starts with a yaml pattern such as:

The portion that I’ll be working on, and want to modify on all the files, is:

What I want it to look like is:

I’ve used regex to match the portion that needs to be changed:

Now, I don’t know how to go forward with this. I was thinking if I could store portion of the matched characters in a variable and use it when I do a replace. If there’s another, easier way, other than variables, I’m all ears.
I’m using Sublime Text. I’m not sure what more information I should write so please ask away.
I also have Keyboard Maestro. Of course Applescript and Automator are given.

Hi af. Welcome to MacScripter.

I’m not clear if you want to modify that section within the text or just keep the modified part. I’ve assumed the former here.

I’m not familiar with Sublime Text. If it has a regex find-and-replace facility like BBEdit’s or TextWrangler’s, you could make the search string .

. or maybe .

. and use this as the replacement string:

If Sublime Text can’t do this, or can but can’t be scripted to do it, there are alternatives in the form of a shell script with “sed” (built into Mac OS X), AppleScript Objective-C (ditto, usable as from Mavericks), or the Satimage OSAX (which has to be downloaded and installed on your machine).

This example with “sed” reads a file and does the replacement you’ve specified. As it stands, it just returns the full, modified text, but it can be altered to save it back to the same file or another. It works if the line breaks are all returns or all linefeeds, but not if they’re a mixture:

set pp to POSIX path of (choose file)

do shell script "<" & quoted form of pp & " sed -En '
# Do a global substitution and quit if the text contains returns. (Assume all line breaks are returns.)
/'$'\\r''/ {
	s/(postheaderimage):[^i]+img: +([^'$'\\r'']+'$'\\r'')[^a]+(alt:[^'$'\\r'']+)/\\1: \\2\\1_\\3/g
	p
	q
}
# Otherwise deal with each line in turn. (Assume all line breaks are linefeeds.)
# If this line is not in a "postheaderimage" block, print it.
/^postheaderimage:$/,/^ +alt:/ !p
# If it IS in a "postheaderimage" block, edit and print it if it begins with "    img" or with "    alt".
/^postheaderimage:$/,/^ +alt:/ {
	/^ +img/ s//postheaderimage/p
	/^ +alt/ s//postheaderimage_alt/p
}'"

Hope this helps. Do ask again if it doesn’t. :slight_smile:

Thanks Nigel.