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

后端 未结 7 2049
梦毁少年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:05

    there's no need for regex. think simple.

    >>> "lalala(I want this part)lalala".split("lalala")
    ['', '(I want this part)', '']
    >>> "lalala(I want this part)lalala".split("lalala")[1]
    '(I want this part)'
    >>>
    

提交回复
热议问题