What the answer to .What would be the result of len(match.groups())of a match of (a)(b(?:c (f)(?:e))? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What the answer to .What would be the result of len(match.groups())of a match of (a)(b(?:c (f)(?:e))?

python

3rd Nov 2017, 10:32 PM
Kabelo Selebi
Kabelo Selebi - avatar
2 Answers
+ 2
@Niawahta: This is not ternary operator, this is regular expression, and that's Python coding because 'len' is a Python built-in function... but 'html' tag is irrelevant ;P @Kabelo Selebi: You doesn't provide all necessary code to explain completly, but, on the main lines: + len() return count of items of a list + guessing 'match' contain a (valid: not None) result of a regular expression on a string, match.groups() will return a tupple with all matching, including captured groups + in regular expressions, captured groups are defined by enclosing things inside parenthesis, and NOT captured groups are parenthesis with content starting by '?:' + at least one implicit captured group exist in any regular expression, as we are enclosing whole regular expression inside a prenthesis pair So, answer to your question is: the result will be the number of captured groups (implicit+explicit) by the regular expression (there are always items for each in the returned tupple, even there's nothing to capture -- then item will be an empty string)... 4 in your case (1 implicit + 3 explicit -- not counted: 2 uncaptured)
4th Nov 2017, 10:51 AM
visph
visph - avatar
+ 1
Um... those aren't Python ternary operators. Those are Java/JavaScript/C++/etc. ternary operators.
4th Nov 2017, 1:40 AM
LunarCoffee
LunarCoffee - avatar