Extract string within parentheses - PYTHON

前端 未结 5 1417
深忆病人
深忆病人 2020-12-16 01:00

I have a string \"Name(something)\" and I am trying to extract the portion of the string within the parentheses!

Iv\'e tried the following solutions but don\'t seem

5条回答
  •  暖寄归人
    2020-12-16 01:44

    You can use re.match:

    >>> import re
    >>> s = "name(something)"
    >>> na, so = re.match(r"(.*)\((.*)\)" ,s).groups()
    >>> na, so
    ('name', 'something')
    

    that matches two (.*) which means anything, where the second is between parentheses \( & \).

提交回复
热议问题