0

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.

Community
  • 1
  • 1
patrick
  • 574
  • 3
  • 10
  • 25

2 Answers2

1

In many regular expression search and replace systems, you can refer to a parenthesized group in the regular expression in the replacement, using \1, \2, etc. to refer to each successive group. So for example, in sed you could do:

sed -e 's/\(^[\t ]*\)\*/\1-/'

I'm not sure if Keyboard Maestro gives you that option. It mentions that it uses ICU regular expressions; if it also uses their replacement options, then you can use $1, $2 etc. to refer to the replacement.

If not, all is not lost. You can use a lookbehind assertion to match the sequence of whitespace before the the asterisk, without including the asterisk as part of the match; then just use a single dash as your replacement:

Search for: (?<=^[\t ]*)\*
Replace with: -
Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • Thank you for the detailed description. Now I know how to do a search and replace with `sed` and will incorporate it in other scripts. By the way, Keyboard Maestro allows using replacements. So after @przemyslaw-kruglej's solution I just needed to enter `$1-$2`. – patrick Oct 26 '13 at 09:04
0

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

Przemyslaw Kruglej
  • 8,003
  • 2
  • 26
  • 41