Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract words between the 2nd and the 3rd comma

Tags:

python

regex

I am total newbie to regex, so this question might seem trivial to many of you. I would like to extract the words between the second and the third comma, like in the sentence:

Chateau d'Arsac, Bordeaux blanc, Cuvee Celine, 2012

I have tried : (?<=,\s)[^,]+(?=,) but this doesn't return what I want...

like image 707
user2999570 Avatar asked Nov 16 '13 15:11

user2999570


1 Answers

data = "Chateau d'Arsac, Bordeaux blanc, Cuvee Celine, 2012"
import re
print re.match(".*?,.*?,\s*(.*?),.*", data).group(1)

Output

Cuvee Celine

But for this simple task, you can simply split the strings based on , like this

data.split(",")[2].strip()
like image 199
thefourtheye Avatar answered Nov 02 '22 23:11

thefourtheye