How Do I Use RegEx on RTF Text and Retain Formatting?

Continuing the discussion from Regex not working:

Hey Steve,

Not with normal text-based find/replace utilities.

Jedit Omega can do regex on styled text, and there are other apps like Tex-Edit Plus (although TEP’s syntax is not fabulous).

One method might be to use the textutil command line utility to convert RTF to HTML, manipulate the HTML, and convert back. (Search the forum for “textutil” and you’ll find a few hits.)

I’m fairly sure this can be done using AppleScriptObjC, although you’d really want to ask Shane Stanley on the LNS Forum.


1 Like

@cvc8445, there are several tools that could be used to accomplish this. One factor in choosing the tool is what apps the source RTF text and the revised text will be used in.

For example, if you are using MS Word, then the Word VBA Find/Replace tool is an excellent candidate. It supports several special forms of find, including RegEx, and it can also find/replace formats easily.

So, if you can layout your complete workflow, including the apps you want to use, then perhaps we can be more helpful.

1 Like

@cvc8445, I’ve developed an AppleScript as proof-of-concept that will change RTF data and retain format. Can you please provide the following:

  1. RTF Source Text file (zipped)
  2. Criteria for identifying the text to be changed. If you already have a RegEx pattern that would be perfect.
  3. RTF File after changes have been made (zipped).

The script is fairly complex, but its use is simple. If you can give me a good test case, I should be able to quickly give you a macro to test.

Does anyone know if @JMichaelTX ever made this script available or shared any of his progress?

I think not.

You can use Keyboard Maestro to work with styled text on the clipboard.


If you want to do something more sophisticated then head on over to the Script Debugger Forum.