Ginger Luo
Ginger Luo

Reputation: 55

python regex match single quote

I try to match single quote in below:

s= "name:'abc','hello'"

but seems the behaviour of match/findall is different:

re.match("\B'\w+'\B", s)   # ===> return None

re.findall("\B'\w+'\B", s)  #===> RETURN ['abc', 'hello']

actually this is caused by single quotes in the string, anyone knows what's going on?

I'm using py2.7.8 in win7.

Upvotes: 2

Views: 1532

Answers (1)

rmunn
rmunn

Reputation: 36688

See https://docs.python.org/2/library/re.html#search-vs-match -- "Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default)."

You're using re.match(); if you switch to re.search(), you'll get the behavior you were expecting.

Upvotes: 7

Related Questions