NBA
NBA

Reputation: 37

How to use Ignore case here?

In my code below:

  d={ 'h' : 'hhh' , 'l' : 'lll'}
  enpattern = re.compile(r'(.)\1?')
  str='HeLlo'
  result = [x.group() for x in enpattern.finditer(str)]
  data=''.join(map(lambda x:D.get(x,x),result))
  print data

This code takes each char from string and compares it with the key in dictionary and gives me the corresponding value. But here How can I ignore the CASE sensitivity when using finditer() method?

Upvotes: 0

Views: 67

Answers (1)

thefourtheye
thefourtheye

Reputation: 239693

As per your comments, you want to get hhhellllllo, so this is what you expect

result = [m.lower() for x in enpattern.finditer(my_string) for m in x.group()]
data   = ''.join(map(lambda x: d.get(x, x),result))

And don't name your variables as str, as that shadows the builtin str function

Note: You don't need RegEx at all to get that output. Its a simple one-liner

print ''.join(map(lambda x: d.get(x, x), my_string.lower()))
# hhhellllllo

Upvotes: 1

Related Questions