DEV Community

Cover image for Regular Expressions for Highlighting Comments in PyCharm
Oleksii Lytvynov
Oleksii Lytvynov

Posted on

Regular Expressions for Highlighting Comments in PyCharm

Students often ask why regular expressions are necessary. At first glance, their usefulness may not be obvious. In general, their purpose is working with text: searching and replacing.

For instance, I once needed to compare logs from two test runs. They were potentially identical, but each line began with timestamps that differed.
Using a regular expression to match the timestamps, I replaced those substrings with an empty string in Sublime (a feature likely available in any text editor). Afterward, I compared the two files in Meld — a handy tool for comparing files and directories that I frequently use.

PyCharm also supports searching and replacing text with regular expressions.

PyCharm Search

However, I’ll discuss another PyCharm feature that utilizes regular expressions: highlighting specific comments in code. The most common example is TODO comments.

TODO comment

But how does PyCharm know to highlight this text in a specific color? These settings can be customized, and that’s what we’ll explore.

Navigate to the menu:
File -> Settings... -> Editor -> TODO

TODO settings

Here, you'll find two predefined rules for highlighting comments: TODO and FIXME. Try it — FIXME uses the same highlighting rule as TODO.
The rule itself is defined by the regular expression: \btodo\b.*.

This pattern matches:

  1. The word todo as a whole word (using \b to denote a word boundary)
  2. Followed by any number of any characters (.*).

A word boundary (\b) is the edge between a \w character (letters, digits, or underscores) and a non-\w character.

In this menu, you can add your own rules. For example, let’s add a rule for "Not implemented".

Adding Not implemented rule

Here, you can configure:

  • Pattern: a regular expression to match the text to highlight.
  • Icon: an icon to display in the TODO tool window.
  • Case sensitivity: whether the match is case-sensitive.
  • Default style: if unchecked, you can customize the highlighting style, otherwise default TODO style will be applied.
  • Custom style options:
    • Font style (bold, italic)
    • Text color
    • Background color
    • Error stripe color
    • Decoration styles and their colors (underscored, bold underscored, underwaved, bordered, strikeout, dotted line).

Here’s what we’ve got:

Not implemented comment

And here’s how it appears in the TODO tool window: you can see the custom icon we selected, and the border color is more prominent. In this screenshot, I’ve also clicked the filters button to show that no filters are applied yet.

TODO tool window

Let’s return to the TODO settings and add a filter for the "Not implemented" rule.

Adding Not implemented filter

Now, in the TODO tool window, a new "Not implemented" filter appears in the list. When you select this filter, only "Not implemented" comments will display in the TODO tool window.

TODO tool window with filter applied

There’s one more option in the TODO comment settings we haven’t mentioned: "Treat indented text on the following lines as part of the same TODO".

This checkbox applies to all rules. If the line following a TODO comment contains an indented comment, the same rule will be applied to it.

TODO comment with indented text


As you can see, there’s no need to create overly complex regular expressions — simple ones will suffice. However, even in this context, they can make your work easier.
Do you use regular expressions often?

Top comments (0)