[SOLVED] Insert character or word(s) at certain position

Working on a macro that allows me to add 1 or more characters at a specific position on 1 or more lines of text. I can't figure it out. For example let's say the line is
"This is a text file"
and I want to insert "normal" before "text" like this
"This is a normal text file"

I was trying RegEx, but can't find anything online and can't figure it out myself.

Any tips?

So my idea is that I have a Prompt action where I can type a number and that will be the position of the extra character or word(s), so for the example above that would be "11"

I think I'm getting there...
I saw the documentation and something referencing the use of something like $1, $2, etc.
I guess those would be capture groups?
Will do some more research...

Perhaps 10 rather than 11 ?

( Regex can sometimes provide a workable last resort, but it's never a good place to start by default, unless you really want to lose a lot of time )

Insert string at index.kmmacros (4.1 KB)


Thank you so much.
I didn't know about Substrings. That's a good thing to know and I already wrote a note about it. I can see how that can be very useful.

RegEx was my first thought and I think I was getting close.
I will still try it, just to make sure my initial idea could work, just because I'm still new to RegEx and it's another exercise.
But yes, your macro makes a lot more sense and I will use that for my macro.

Appreciate it! :raised_hands:

1 Like

Rob, you are hilarious with your disdain for Regex! :rofl:

1 Like

I had huge fun, in 1997, working through the first edition (now in its third) of Jeffrey Friedl's excellent Mastering Regular Expressions, and at the time they transformed my use of Microsoft Word, and allowed me (with Perl) to solve problems with the formatting of data files with 40M records, but ...

the final stage of mastering them is realising that they are best used very occasionally, and in very simple combinations. Any scripting language which provides splitting, indexing and recursion will tend to give you much quicker and more readable solutions.

1 Like