capturing-group

Why does re.sub replace the entire pattern, not just a capturing group within it?

Why does re.sub replace the entire pattern, not just a capturing group within it? Question: re.sub(‘a(b)’,’d’,’abc’) yields dc, not adc. Why does re.sub replace the entire capturing group, instead of just capturing group'(b)’? Asked By: Nick || Source Answers: Because it’s supposed to replace the whole occurrence of the pattern: Return the string obtained by …

Total answers: 4

What does this Django regular expression mean? `?P`

What does this Django regular expression mean? `?P` Question: I have the following regular expression (regex) in my urls.py and I’d like to know what it means. Specifically the (?P<category_slug> portion of the regex. r’^category/(?P<category_slug>[-w]+)/$ Asked By: locoboy || Source Answers: (?P<category_slug>) creates a match group named category_slug. The regex itself matches a string starting …

Total answers: 5