Extract only first match using python regular expression

后端 未结 3 570
长发绾君心
长发绾君心 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
    
    0 讨论(0)
  • 2021-01-28 06:33

    You can use str.replace() :

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

    edit: if you want to replace the word before (Online) you need regex and a positive look-behind:

    >>> re.sub(r'(\(\w+\) )(?=\(Online\))','',course_name)
    'Post Graduate Certificate Programme in Retail Management (Online)'
    

    Or if you want to remove the first parentheses use following :

    >>> re.sub(r'(\(\w+\) ).*?','',course_name)
    'Post Graduate Certificate Programme in Retail Management (Online)'
    

    and for extract it use re.search :

    >>> re.search(r'(\(.*?\))',course_name).group(0)
    '(PGCPRM)'
    
    0 讨论(0)
  • 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)'
    
    0 讨论(0)
提交回复
热议问题