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.
However, I’ll discuss another PyCharm feature that utilizes regular expressions: highlighting specific comments in code. The most common example is TODO comments.
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
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:
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".
Here, you can configure:
Here’s what we’ve got:
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.
Let’s return to the TODO settings and add a filter for the "Not implemented" rule.
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.
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.
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!