Extract string within parentheses - PYTHON

前端 未结 5 1423
深忆病人
深忆病人 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:48

    You can use a simple regex to catch everything between the parenthesis:

    >>> import re
    >>> s = 'Name(something)'
    >>> re.search('\(([^)]+)', s).group(1)
    'something'
    

    The regex matches the first "(", then it matches everything that's not a ")":

    • \( matches the character "(" literally
    • the capturing group ([^)]+) greedily matches anything that's not a ")"

提交回复
热议问题