Because the meaning of * in r'(d*)' is to match 0 or more, it can not match any characters. Use re.search to start matching from the beginning of the string. Because the first character of 'thenextnothingis123456' is not a number, it cannot be matched, but r'(d*)' can not match any characters, so an empty string is returned.
And r'(d+)' requires matching 1 to multiple numbers. When used to match 'thenextnothingis123456', it is found that the first character is not a letter, and it will continue to try the second character until character 1 starts with numbers. So "123456" is matched. You can understand it by looking at the output below.
>>> import re
>>> text = 'thenextnothingis123456'
>>> p = re.search(r'(\d*)', text)
>>> p.start()
0
>>> p.end()
0
>>> p.groups()
('',)
>>> p = re.search(r'(\d+)', text)
>>> p.start()
16
>>> p.end()
22
>>> p.groups()
('123456',)
I feel that it has actually been matched, which is the number of 0 times: Your .group(0) did not report a nonetype error, indicating that the match was successful
Because the meaning of * in r'(d*)' is to match 0 or more, it can not match any characters. Use re.search to start matching from the beginning of the string. Because the first character of 'thenextnothingis123456' is not a number, it cannot be matched, but r'(d*)' can not match any characters, so an empty string is returned.
And r'(d+)' requires matching 1 to multiple numbers. When used to match 'thenextnothingis123456', it is found that the first character is not a letter, and it will continue to try the second character until character 1 starts with numbers. So "123456" is matched. You can understand it by looking at the output below.
Why can’t this code match numbers?
I feel that it has actually been matched, which is the number of 0 times: Your .group(0) did not report a nonetype error, indicating that the match was successful
30-minute introductory tutorial on regular expressions
11111111 The result of this sentence is empty because re.search uses #######2222222
You try d, and * means 0 or more times