Bunny Rabbit
Bunny Rabbit

Reputation: 8411

Look for multiple occurances of a character using regex

i using the pattern pat='dd|dddd' , and i thought it would either match dd or dddd.

import re
re.search(pat,'ddd')
re.search(pat,'ddddd')

any number of d(s) matches for that matter why is it so ?

Upvotes: 1

Views: 458

Answers (2)

MAK
MAK

Reputation: 26586

As already pointed out by Charles Duffy, search isn't really the function that you should be using. Try using match or even findall.

>>> import re
>>> re.match('dd|dddd','dd').group()
'dd'
>>> re.findall('dd|dddd','dd')
['dd']
>>> re.match('dd|dddd','ddddd').group()
'dd'
>>> re.match('dddd|dd','ddddd').group()
'dddd'

Upvotes: 2

OmnipotentEntity
OmnipotentEntity

Reputation: 17131

You'll need to anchor the regular expression somehow. A regular expression searches within strings to find a pattern. So "dd" will be found in "dddddddd" at offset 0,1,2,3,4,5,6.

If you want to match only entire strings try ^dd$. ^ matches the beginning of a string, $ matches the end. So ^(dd|dddd)$ will have the behavior you want.

If you want it to match only dd or dddd but within a string. Then you might want to use: [^d](dd|dddd)[^d] Which will match "anything that isn't d" then either two or four ds then "anything that isn't d"

Upvotes: 10

Related Questions