Sam
Sam

Reputation: 2605

Regex to capture string between quotes especially when the string starts with a quote

I have a string as given below.

string= 'Sam007's Helsen007' is a 'good' boy's in 'demand6's6'.

I want to extract the string inside the quotes.

The output should looks like,

['Sam007's Helsen007', 'good', 'demand6's6']

The regex I have written in :

re.findall("(?:[^a-zA-Z0-9]*')(.*?)(?:'[^a-zA-Z0-9*])", text)

But this gives output

["Sam007's Helsen007", 'good', "s in 'demand6's6"]

when I use modify the regex to

re.findall("(?:[^a-zA-Z0-9]')(.*?)(?:'[^a-zA-Z0-9*])", text)

It gives me an output:

['good', "demand6's6"]

The second case seems more appropriate, but it cant handle the case if a string is starting with a quote.

How can I handle the case.

Upvotes: 2

Views: 61

Answers (1)

vks
vks

Reputation: 67988

st= "'Sam007's Helsen007' is a 'good' boy's in 'demand6's6'"


print re.findall(r"\B'.*?'\B",st)

Use \B i.e non word boundary

Output:["'Sam007's Helsen007'", "'good'", "'demand6's6'"]

If you look carefully through your string you want a string ' which has a non word character before and ' which has a non word character after.

Upvotes: 6

Related Questions