How can I get part of regex match as a variable in python?

后端 未结 7 2015
梦毁少年i
梦毁少年i 2020-11-29 07:19

In Perl it is possible to do something like this (I hope the syntax is right...):

$string =~ m/lalala(I want this part)lalala/;
$whatIWant = $1;
相关标签:
7条回答
  • 2020-11-29 08:17

    If you want to get parts by name you can also do this:

    >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcom Reynolds")
    >>> m.groupdict()
    {'first_name': 'Malcom', 'last_name': 'Reynolds'}
    

    The example was taken from the re docs

    0 讨论(0)
提交回复
热议问题