Here it is:
import re
>>>s = \'abc -j k -l m\'
>>>m = re.search(\'-\\w+ \\w+\', s)
>>>m.groups()
()
>>> m.group(0)
\'-j k\'
<
groups()
only returns any explicitly-captured groups in your regex (denoted by (
round brackets )
in your regex), whereas group(0)
returns the entire substring that's matched by your regex regardless of whether your expression has any capture groups.
The first explicit capture in your regex is indicated by group(1)
instead.
Re follow-up edit:
Why can't
search
give me all the substrings?
search()
will only return the first match against the pattern in your input string.