I have found RegEx to be the epitome of “use it or lose it”. While I use RegEx often for my own personal use, much of it is in response to questions like yours. So I was able to create the RegEx patterns for this macro from memory.
I make great use of RegEx101.com. I do all of my RegEx development and testing there. And even for “simple” RegEx patterns, which I think I know, I always test them there first before using/publishing.
Here are the snippets I used for this macro:
Extract Text Blocks as Records
Extract Fields from Text Record Block
Pretty much everything there is very common RegEx. Perhaps less common is use of the
s flag in
(?ms). This makes the dot meta character match newlines as well.
One tip is that sometimes it is easier to use multiple passes of RegEx rather than trying to do everything in one large RegEx. In this case it really made sense to do a simple RegEx to extract the block of text (as “record”), then use another RegEx to extract the fields from that block.
If you have any specific questions, I’d be glad to try to answer.
I assume you mean MS Word “.docx” files, which are actually text XML files. That would be more challenging, and maybe even an entirely different approach. However, one simple workaround is to simple use a “.docx” extension when you save the file. Then when you open it in Word, Word will prompt to convert from RTF.
That is a lot more complex. KM can write any file, but you have to built the contents. So, one approach would be to build a Word .docx file, and then open it up in a text editor to find where the heading is applied. If you use a placeholder, like “
[[PLACE]]”, then you could use KM to replace the placeholder with the actual data, and then write that out to a file with a .docx extension.
That is all very much just an idea/theory, as I have not actually done any of that. And, it is, of course, left as an “exercise for the student”.