regexreplaceindentationkeyboard-maestro

Is there a smarter way to keep the indentation when replacing characters with a regex?


I want to replace the asterisks in a Markdown list with hyphens.

Example:

  • 1.0
    • 1.1
    • 1.2
  • 2
    • 2.1
      • 2.2

Currently I have a separate regex pattern for up to three levels of indentation set up in Keyboard Maestro for Mac:

Keyboard Maestro Search and Replace with a Regex Pattern

I wonder if there isn't a smarter way to do this and which adresses all kinds of indentation.


Solution

  • You can use submatching groups and reference them in the replacing string like this:

    Regular expression matching your lines with list items: ([\t ]*)\*(.*)

    The string used for replacement: \1-\2