There he is:
import re
>>>s = 'abc -j k -l m'
>>>m = re.search('-\w+ \w+', s)
>>>m.groups()
()
>>> m.group(0)
'-j k'
Why groups()gives nothing, but group(0)gives some? What is the difference?
Follow up
The code is as follows
>>>re.findall('(-\w+ \w+)', s)
['-j k', '-l m', '-n o']
findallcan get all the substrings -\w+ \w+, but look at this:
>>>m = re.search('(-\w+ \w+)+', s)
>>>m.groups()
('-j k',)
Why searchcan't give me all the substrings?
Repeat again
If s = 'abc -j k -l m -k oand
>>>m = re.search(r'(-\w+ \w+ )+', s)
>>>m.groups()
('-l m ',)
>>>m.group(0)
'-j k -l m '
source
share