Search and replace paragraph mark in clipboard text?

I am using the latest version of Keyboard Maestro and was wondering how to search the clipboard for the paragraph mark and replace all instances with two paragraph marks? Thanks.

Hey Meg,

Try it in your favorite text editor.

Select some text, and run the macro.

Oh, I've set it to find 1 or more carriage returns (or newlines) and turn them into 2 to regularize the output.

You can remove the + sign from the regular expression to replace each and every carriage return (or newline) with two.

-Chris


RegEx { Copy & Replace Text }.kmmacros (2.8 KB)

2 Likes

That worked perfectly. Thanks! :smile:

This is exactly what I was looking for! Thank you so much!

Anyone advise. Got this far but cannot get rid of first and last paragraph marks using regex.

Cheers

Michael

If this is MS Word, then use this AppleScript, which I copied from here, page 119:

ftp://ftp.mactech.com/pri/article-submissions/07-ArticleGalleysToReview/!Archived%20Files/old%20versions%20of%20VBA%20guide/23.04%20VBA-Chapter%203.pdf

(FTP links don’t seem to work here.)


Delete Empty Lines (MS Word).kmmacros (3.2 KB)


What regex and app are you using?

Hey Michael,

When asking for help with regular expressions it's a good idea to post the actual text and regex you're working with.

It's also a good idea to say what application you're working in, because it sometimes makes a great deal of difference in the approach used for the problem.

Tom's method is nice, because it works directly with Word to get the job done.

For something more generic try this macro:

RegEx → Word → Test KM Find & Replace RegEx.kmmacros (2.2 KB)

NOTE → You need to select the text you're working with prior to running the macro.

-Chris

1 Like

Apologies for confusing.

Text shows what is left after regex in textedit BUT copied and pasted into Word in order for me to see what the white spaces beginning and ending were. Below is original

and below is result so far

So by pasting into Word I know that leading and trailing white spaces are paragraph characters but a) cannot find specifically the regex symbol for paragraph mark and b) how to delete leading and trailing occurrences.

Thanks

Michael

Hey Michael,

Well, I showed you how to do exactly that.

You still haven’t provided the actual test-case text and the regular expression (or macro) involved.

Without seeing those it’s hard to tell where you’ve gone wrong.

-Chris

Sorry Chris

thought that pertained to Word - just tried and great - works. Also, streamlined other bits.

What is the vertical line after the first \s “|” called so I can look it up?

Cheers

Michael

Hey Michael,

“|” is often called the bar or “or” symbol and its function in a regular expression is alternation (a logical OR).

Since it is the pipe symbol in shell scripting it is sometimes referred to as that in regex (although the meaning is very different).

Alternation – alternation allows several subexpressions to be tested at a given point. Each subexpression is called an alternative.

* From “Mastering Regular Expressions” by Jeffrey Friedl.

^Chris|Christopher.+

This pattern will find lines beginning with “Chris” OR “Christopher”

-Chris

Whups – I miswrote that.

The given expression will find lines that:

Start with Chris

Contain Christopher and 1 or more characters.

I meant to write it this way:

^(Chris|Christopher).+

This will find lines that start with Chris OR Christopher and contain 1 or more additional characters.

-Chris

I have a question - I’m trying to delete the lines in document that contain only 1 or more spaces (typically just one). The “Trim Whitespace” in “filter variable” does not take them out. Any hints?

I’m using the macro: Author.@cfriend PURPOSE: Remove blank lines between lines of highlighted text

An example of the type of text that I am trying to clean up is. This happens sometimes when I paste text from an Office document into Apple Mail, even if I paste as plain text.

Test test text

Test Test test text

TestTest test text

Test Test test text
Test
Test
Test
Test

This RegEx should do the trick:
SEARCH FOR:
(?m)^\s*\n

REPLACE WITH:
<blank>

Here's a example macro. Adjust as needed:

Example Results

MACRO:   Remove Blank Lines (Zero or more white space)


VER: 1.0    2017-02-18 ~~~

DOWNLOAD:

Remove Blank Lines (Zero or more white space).kmmacros (2.7 KB)


2 Likes

THANK YOU. Works perfectly. I speak a few foreign languages (not programming) and learning Applescript and Expressions seem a lot like learning a new language to me! I appreciate your help very much.

1 Like