count instead of max | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

count instead of max

Hello, shouldn't the right definition in the course (Regular Expressions -> Introduction -> Search&Replace) be def re.sub(pattern, repl, string, count) rather than the mentioned def re.sub(pattern, repl, string, max)? If not, may I kindly ask you to explain why? Thanks for checking!

19th Feb 2017, 3:20 PM
Margarete Müller
Margarete Müller - avatar
4 Answers
+ 2
The easiest way to work with it is to write re.sub(..., 1) if you want to replace the first pattern. But you are right, it works with count=1 instead of max=1
27th Feb 2017, 10:04 AM
Anselm
+ 1
The mentioned "max=0" is correct. re.sub(...) doesn't count how often you replace a pattern. The max value can be used to set the maximal number of patterns to be replaced. If max=0, all patterns are replaced
26th Feb 2017, 9:28 PM
Anselm
0
Thanks for your reply. When using max the code shows an error message. Replacing max with count works. That's what confuses me. Have you checked the code in the course example?
26th Feb 2017, 9:38 PM
Margarete Müller
Margarete Müller - avatar
0
Thanks. I think I'll use the shorter version :-)
27th Feb 2017, 6:09 PM
Margarete Müller
Margarete Müller - avatar