For Each Paragraph

hey! I know we can do For loops for lines of text. Any way to use a For loop to process a series of paragraphs separated by a double return? Thanks in advance :smile:

Hey Joel,

What is the source of the text?

Best Regards,

A bunch of text copied to the clipboard (does that answer the question?) Thanks

Hey Joel,

Easier to use Search & Replace on the Clipboard with regular expressions then a for-loop.


Text { Copy to Clipboard, Replace with RegEx, Paste }.kmmacros (2.8 KB)

Thanks for your help Chris, regex is my weakness so I’m glad you’re here :slight_smile:

I just tried what you posted, but it just takes all the paragraphs and allows all the lines to be processed together. I probably didn’t explain using the right terminology so I made it confusing…

I should probably clarify the way I am defining “lines” and “paragraphs” since in this post it may get confusing.

Line = separated by 1 return space
Paragraph = a break in the series of lines separated by 2 returns (an empty line is between each paragraph)

What I need is to have each paragraph processed separately:

So if my variable "temp"contains 2 paragraphs like this:

*Paragraph 1 Line 1 blalablabla
P1L2 blablablabla
P1L3 blablabla

Paragraph 2 Line 1 blalablabla
P2L2 blablablabla
P2L3 blablabla*

I need to use a For Loop to process the lines of Paragraph 1, then after that process the lines of Paragraph 2. This is instead of joining all paragraphs together and processing all lines together. I guess it would be a For Loop within a For Loop.

It may sound silly, but I need to run actions after each paragraph before proceeding onto processing the next paragraph.

Currently I have a macro capturing each paragraph separately in excel and then processing the lines in a For Loop. Then after the For Loop finishes, it dives back into excel and captures the next paragraph, processing the lines etc etc.

What I am hoping is to speed this macro up significantly by being able to select ALL of the sets of lines (paragraphs) and process them separately rather than merging all the lines together. If I could do this, then I could eliminate the need to have the macro go back into excel to capture every paragraph individually.

I hope I’m explaining myself better… I have proofed this a couple times now to try to clarify as much as I can to not waste your time :slight_smile:

Thanks again for your help!

Yes. You need to make sure there is a pair of newlines after every paragraph including the last one.

Thank you so much for this Peter!! This is invaluable to me and I would have never been able to come up with it on my own. I just tested it and works exactly as I needed.
I’m learning little by little about the power of Regex, but it ain’t easy! Have a long way to go.
Thanks a million,

I’m happy to help. Regex is a little like Keyboard Maestro - the basic concept is relatively straight forward, and each component is orthogonal, so each part you learn adds to all the others.

The ICU Regular Expression Reference is linked from the Keyboard Maestro Help menu and lets you see the different components of Regex. Its quite helpful when trying to remember which magic code matches what.