Can Someone Help Me Figure Out How to Separate Sentences Using RegEx?

I know nothing about reg ex but would like to create a macro that can place a divider between each sentence. How could I achieve this?

What I am trying to do basically is search a body of text using this reg ex pattern to automatically place a pause marker between each sentence for a speech synthesizer.

The basic problem that you are facing is that RegEx deals with characters. And there is no character that designates the end of a sentence. The end of a paragraph is a different matter. The end of a paragraph is a \n (new line) character.

That being the case, you will not find a perfect answer. The question is then can you find an acceptable answer. That would depend on the text that you are trying to process and the level of perfection you require.

Commonly sentences end with a period (.) or a question mark (?) or an exclamation mark (!) followed by a space. So a simplistic Reg Ex pattern to look for this would be (. |? |!|\n) This pattern would match a period followed by a space OR ? followed by a space or an ! followed by a space or the end of a paragraph. However there will be false positives like Mrs. Jones. The period at the end of Mrs. will be recognized as the end of a sentence. There will be lots of problems. Some text might have the convention that the typist puts TWO spaces at the end of a sentence. That might make things easier but that convention is pretty much in the rear-view mirror.

In any case, if you want to play with Reg Ex, you would not be starting with Keyboard Maestro. Get a text tool like BBEdit that understands Reg Ex and play with that. If you find something that works, then you might want to move the project to Keyboard Maestro although often the task will be already "done" just using the text editor.

You can use a text editor like BBEdit to do a Search and Replace so if you found an acceptable pattern to recognize the end of a sentence, you could replace that pattern with whatever character you are using as a pause marker.

The discussion here may be useful: https://stackoverflow.com/questions/4576077/how-can-i-split-a-text-into-sentences

1 Like

Hi @Barlow - searching this forum for regex sentences pulled up two discussions that might help you:

And

Hope you find them interesting!

Hey @Barlow,

Welcome to the forum!   :smile:

You haven't defined your task very precisely.

Sentences can be presented in so many different ways, and when working with regular expressions the devil is in the details.

If you haven't read these they're worth a couple of minutes of your time.

Tip: How Do I Get The Best Answer in the Shortest Time?

How to Post Your Macro to the Forum

If you're still having problems with this task report back, and we'll help you out.

-Chris