In this short guide, I’ll show you Regular Expressions in Sublime 3 with examples: Search and Replace, Regex Multiline search, Regex Groups and more. Sublime and Regex replace is perfect for data changes on small to medium datasets.

Sublime Text 3 Tricks: Compare Files, Highlight and Regex

Video - 5. Sublime 3 REGEX Search, Replace, Groups Examples

Step 1: How to use Regex in Sublime to find text

To start, you need to enable Regex search in Sublime by:

  • Open Sublime
  • Open Find or Replace pane
    • Find - CTRL + F
    • Replace - CTRL + H
  • Enable Regular Expressions - ALT + R
  • Type your regular expression
    • Searching for words starting with capital P - P[\w]+
    • Search for coma and new line - ,\n
  • Verify the results - they will be highlighted in the Text. Check the image below.

Sublime_Regex_Search_Replace

Step 2: Sublime Search and Replace with Regular Expression

So far we know how to find text by using regex patterns. Based on that we are going to find text and replace it by another. An example from the real world is to find all email addresses and replace them with: [email protected]. So first we need to start Replace panel and enter the pattern for email address:

[\w\-\.]+@([\w-]+\.)+[\w\-]{2,4}

Before:

email1: [email protected]
email2: [email protected]
email3: [email protected]

After:

email1: [email protected]
email2: [email protected]
email3: [email protected]

This is a very simple pattern for email address which might need adjustments for some cases.

Then you need to add the replacement value: [email protected]. Now you can verify the matches:

  • there is number which shows the count of found items
  • check highlighted values in the text

When you are happy with results you can proceed with replacement:

  • Replace - one by one
  • Replace All - all at once

Another useful example is search and replace for dates with regex:

Before:

26 February 2020

After:

date

  • Search: [\d]{1,2} [ADFJMNOS]\w* [\d]{4}
  • Replace: date

If you need more examples about dates you can find it here: Python Regex Match Date

Step 3: Sublime Regex Search and Replace groups

Next example shows how to use regex groups in order to find something and use it in the replace statement. Lets have this text:

Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991. Python's

Our goal is to replace all matches of Python with Python 3. This can be done by:

  • Search for (Python)
  • Replace with: $1 3

More advanced example is switching values of a dict with Regex in Sublime(or any pair of values) by. Lets have this example:

'Wednesday': 2,
'Tuesday': 1,
'Saturday': 5,

our goal is to achieve:

2: 'Wednesday',
1: 'Tuesday',
5: 'Saturday',

  • Search: (.*): (\d)
  • Replace: $2: $1

Another interesting examples is changing date formats like:

Before:

dd/mm/yyyy

After:

yyyy-mm-dd

Changing date format with Regex in Sublime is simple as:

  • Search: ([\d]{1,2})/([\d]{1,2})/([\d]{4})
  • Replace: $3-$2-$1

One more interesting example is replacing HTML tags - because it includes groups and or.

Before:

<p> Python 1 </p>
<p> Python 2 </p>

After:

<li> Python 1 </li>
<li> Python 2 </li>

This can be achieved by:

  • Search: (<)(.|)p
  • Replace: $1$2li

Sublime_Regex_Search_Replace

Step 4: Sublime Search and Replace multiple lines

Common problem for beginners and Regular Expressions is multiline match. IN the next example we can find how to perform multiline match for XML/HTML text like:

<br>
	<ul>
	    <li> Python 1 </li>
	    <li> Python 2 </li>
	    <li> Python 3 </li>
	    <li> Python 4 </li>
	</ul>
</br>

If you like to find and replace everything between <ul> and </ul> Then you can use this pattern: <ul>\n(^.+$\n)+?.*</ul>.

Removing several lines at once from Markdown list like:

Before:

  • Markdown list
  1. First ordered list item
  2. Another item
    ⋅⋅* Unordered sub-list.
  3. Actual numbers don't matter, just that it's a number
  4. Ordered sub-list
  • And another item.

After:

  • Markdown list
    ⋅⋅* Unordered sub-list.
  • And another item.
  • Search for \d.\s.*\n\d..*
  • Replace with: `` - nothing

Step 5: Sublime Regex Find and Remove before

In this example we can see how to find a match and remove the text before the match while keeping the match. For example:

Before:

Duck, dynamic, gradual (since 3.5)

After:

Duck, dynamic, gradual (3.5)

This can be achieved with groups and replace:

  • Search: \w+ (\d\.\d)
  • Replace: $1

Bonus: Sublime Regex Cheatsheet

Below you can find the Regex cheatsheet for Sublime 3 which is applicable even beyond Sublime context. The first table cover special characters and their meaning:

Most popular Regex Characters

expression Description
. Match any character
^ Match line begin
$ Match line end
* Match previous RE 0 or more times greedily
*? Match previous RE 0 or more times non-greedily
+ Match previous RE 1 or more times greedily
+? Match previous RE 1 or more times non-greedily
? Match previous RE 0 or 1 time greedily
?? Match previous RE 0 or 1 time non-greedily
`A B` Match either RE A or B

Popular Regex Classes

class name Description
\d Equal to [[:digit:]]
\l Equal to [[:lower:]]
\u Equal to [[:upper:]]
\s Equal to [[:space:]]
\w Equal to [[:word:]]
\W Equal to [^[:word:]]
\D Equal to [^[:digit:]]
\L Equal to [^[:lower:]]
\U Equal to [^[:upper:]]

Regex Character sets

expression Description
[abc] Match either a, b or c
[^abc] Match any character except a, b and c
[a-z] Match the range from a to z
[b-e2-7] Match the range from b to e or the range from 2 to 7
[a-z] Match a, - or z
[a-] Match a, -