Regular Expressions for Highlighting Comments in PyCharm

Susan Sarandon
Release: 2024-11-21 19:22:19
Original
857 people have browsed it

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.

Regular Expressions for Highlighting Comments in PyCharm

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

Regular Expressions for Highlighting Comments in PyCharm

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

Regular Expressions for Highlighting Comments in PyCharm

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: btodob.*.

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".

Regular Expressions for Highlighting Comments in PyCharm

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:

Regular Expressions for Highlighting Comments in PyCharm

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.

Regular Expressions for Highlighting Comments in PyCharm

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

Regular Expressions for Highlighting Comments in PyCharm

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.

Regular Expressions for Highlighting Comments in PyCharm

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.

Regular Expressions for Highlighting Comments in PyCharm


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?

The above is the detailed content of Regular Expressions for Highlighting Comments in PyCharm. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template