Extract only first match using python regular expression

后端 未结 3 569
长发绾君心
长发绾君心 2021-01-28 05:50

I have a string as follows:

course_name = \"Post Graduate Certificate Programme in Retail Management (PGCPRM) (Online)\"

I want to extract only

3条回答
  •  盖世英雄少女心
    2021-01-28 06:44

    To extract value within first parenthesis

    >>> course_name = "Post Graduate Certificate Programme in Retail Management (PGCPRM) (Online)"
    >>> x = re.search(r'\(.*?\)',course_name).group()
    >>> x
    '(PGCPRM)'
    

    And then to replace

    >>> course_name.replace(x,'')
    'Post Graduate Certificate Programme in Retail Management  (Online)'
    

提交回复
热议问题