python - extract string within string with out double quote -


this learning purpose, have following code. if wanna extract 'abcbc' out double quote? tried re.search(r'\a\"(.*?)\"',a).group() noting change.

>>> = "\"abcbc\" lol" >>> re.search(r'\a"(.*?)"',a).group() '"abcbc"' 

also if change pattern '\a"(.*?)' should return after double quote? gives following. there wrong?

>>> re.search(r'\a"(.*?)',a).group() '"' 

you capturing want in group, calling group() no arguments, returning entire match (group 0), not group want (which group 1). call .group(1) return first group, has want.

>>> = "\"abcbc\" lol" >>> re.search(r'\a"(.*?)"',a).group(1) 'abcbc' 

as second question, *? non-greedy: match little can. since * allows matching zero, *? match nothing if can --- , can, since don't have after force match point. quote after it, match little can provided matches quote next, forces keep consuming text until reaches close quote. without quote, matches nothing, since has no reason continue matching @ all.

if use non-greedy one, indeed match after double quote --- everything after double quote, end of line:

>>> re.search(r'\a"(.*)',a).group() '"abcbc" lol' 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -