Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does (f'^\\s*{args.comment}') match in python regex?

I found this line in some Python code, but I can’t seem to understand what it means. The purpose of the script is to count the amount of lines of code from STDIN. The args.comment is the comment identifier that you give (so it won’t count these lines). Can someone explain what this regex will match?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

That is a formatted string literal, introduced in Python 3.6. Backslashes need to be escaped in strings because otherwise they are interpreted as an escape sequence (e.g., \n, \t).

The author could have instead used a formatted raw string (fr'...'), like this:

fr'^\s*{args.comment}'

In a raw string, backslash has no special meaning, so it can be used verbatim.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading