I have a string of pattern {{Start date|2005|8|29}}
Now I want to extract 2005,8, 29 as three separate elements.
Hence the regexp I used: re.findall(‘([0-9])+’,str)
this is giving me 5,8 and 9 instead of 2005, 8, 29
Using same pattern when I tried search method re.search(‘([0-9])+’,str).group()
then I got the first element as 2005.
Why there is difference between these 2 method’s output. When should be the regex for my current date pattern?
I have a string of pattern {{Start date|2005|8|29}} Now I want to extract 2005,8,
Share
Long story short: you meant
'([0-9]+)', not'([0-9])+'.Details: Every pair of parens determines a group. If, in a single attempt to match, the group is captured multiple times, only the last capture is reported.
In particular, when you called this re against the string, it matched all of
"2005"on the first go round, because([0-9])portion matched'2', then'0', then'0', then'5', before it couldn’t match any more. So it matched all of"2005", but the group itself only reported the last match for that subexpression:'5'.In comparison, later you tried
re.searchwith the group method. The group method returns the substring matched by the entire regexp, not the first group. So it returned all of"2005". Try calling.group(1)instead to see the difference.