This page describes how to use regular expressions in a text editor to convert a CSV table to Markdown format. This is a simple use case for regular expressions in search and replace. The aim is not to achieve great efficiency, just to get in some regular expression practice!
Make a backup of your CSV file!
Open your CSV file in an editor that supports regular expressions, such as Sublime Text.
Select the Regex option for search and replace.
Replace all commas with pipes.
Search for: ,
Replace with: |
Add a pipe and space before the first character in the line.
Search for: ^(.)
Replace with: | $1
To conserve the first character we reference a group defined by parentheses around the wildcard for any character (which is a dot) in the first position which is defined by the caret symbol (^).
Add a space and a pipe after the last character in the line.
Search for: (.)$
Replace with: $1 |
Here the character we conserve is in the last position in the line, which is defined by the dollar sign ($).
Duplicate the header line, so you have two headers on line 1 and line 2 of the file.
Select the new line and replace each set of characters in between the pipes with six dashes to complete the Markdown header.
Select the option to search in the selection.
Search for: ([\w\s\-\_]+?)(\|)
Replace all in the selection with: ------$2
This matches two groups. The first group is made up of word, space, dash, and underscore characters. There can be one or more of these characters and the matching continues until the next part of the regular expression is found. The second group is a pipe character. To search for a pipe we need to use a backslash before it so that it is interpreted as pipe and not a special character. We replace the first group with six dashes and preserve the second group.
Check the text and if it is all right, copy and paste it into your Markdown file.