Extract only first match using python regular expression

后端 未结 3 571
长发绾君心
长发绾君心 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:33

    Pretty simple:

    In [8]: course_name
    Out[8]: 'Post Graduate Certificate Programme in Retail Management (PGCPRM) (Online)'
    
    In [9]: print re.sub('\([A-Z]+\)\s*', '', course_name)
    Post Graduate Certificate Programme in Retail Management (Online)
    
    In [17]: print re.search('\(([A-Z]+)\)\s*', course_name).groups()[0]
    PGCPRM
    

提交回复
热议问题