Extract Only First Match Using Python Regular Expression
I have a string as follows: course_name = 'Post Graduate Certificate Programme in Retail Management (PGCPRM) (Online)' I want to extract only the 'PGCPRM' or whatever within the v
Solution 1:
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
Solution 2:
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)'
Solution 3:
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)'
Post a Comment for "Extract Only First Match Using Python Regular Expression"