search
HomeBackend DevelopmentPython TutorialRegular 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.

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!

Statement
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
Why are arrays generally more memory-efficient than lists for storing numerical data?Why are arrays generally more memory-efficient than lists for storing numerical data?May 05, 2025 am 12:15 AM

Arraysaregenerallymorememory-efficientthanlistsforstoringnumericaldataduetotheirfixed-sizenatureanddirectmemoryaccess.1)Arraysstoreelementsinacontiguousblock,reducingoverheadfrompointersormetadata.2)Lists,oftenimplementedasdynamicarraysorlinkedstruct

How can you convert a Python list to a Python array?How can you convert a Python list to a Python array?May 05, 2025 am 12:10 AM

ToconvertaPythonlisttoanarray,usethearraymodule:1)Importthearraymodule,2)Createalist,3)Usearray(typecode,list)toconvertit,specifyingthetypecodelike'i'forintegers.Thisconversionoptimizesmemoryusageforhomogeneousdata,enhancingperformanceinnumericalcomp

Can you store different data types in the same Python list? Give an example.Can you store different data types in the same Python list? Give an example.May 05, 2025 am 12:10 AM

Python lists can store different types of data. The example list contains integers, strings, floating point numbers, booleans, nested lists, and dictionaries. List flexibility is valuable in data processing and prototyping, but it needs to be used with caution to ensure the readability and maintainability of the code.

What is the difference between arrays and lists in Python?What is the difference between arrays and lists in Python?May 05, 2025 am 12:06 AM

Pythondoesnothavebuilt-inarrays;usethearraymoduleformemory-efficienthomogeneousdatastorage,whilelistsareversatileformixeddatatypes.Arraysareefficientforlargedatasetsofthesametype,whereaslistsofferflexibilityandareeasiertouseformixedorsmallerdatasets.

What module is commonly used to create arrays in Python?What module is commonly used to create arrays in Python?May 05, 2025 am 12:02 AM

ThemostcommonlyusedmoduleforcreatingarraysinPythonisnumpy.1)Numpyprovidesefficienttoolsforarrayoperations,idealfornumericaldata.2)Arrayscanbecreatedusingnp.array()for1Dand2Dstructures.3)Numpyexcelsinelement-wiseoperationsandcomplexcalculationslikemea

How do you append elements to a Python list?How do you append elements to a Python list?May 04, 2025 am 12:17 AM

ToappendelementstoaPythonlist,usetheappend()methodforsingleelements,extend()formultipleelements,andinsert()forspecificpositions.1)Useappend()foraddingoneelementattheend.2)Useextend()toaddmultipleelementsefficiently.3)Useinsert()toaddanelementataspeci

How do you create a Python list? Give an example.How do you create a Python list? Give an example.May 04, 2025 am 12:16 AM

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

Discuss real-world use cases where efficient storage and processing of numerical data are critical.Discuss real-world use cases where efficient storage and processing of numerical data are critical.May 04, 2025 am 12:11 AM

In the fields of finance, scientific research, medical care and AI, it is crucial to efficiently store and process numerical data. 1) In finance, using memory mapped files and NumPy libraries can significantly improve data processing speed. 2) In the field of scientific research, HDF5 files are optimized for data storage and retrieval. 3) In medical care, database optimization technologies such as indexing and partitioning improve data query performance. 4) In AI, data sharding and distributed training accelerate model training. System performance and scalability can be significantly improved by choosing the right tools and technologies and weighing trade-offs between storage and processing speeds.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft