Tom McKenna
Tom McKenna

Reputation: 439

Find a word inbetween other letters Python

I need my program to be able to distinguish a word even when it has a few characters inbetween some of the letters. For example, say it was given "guitar". I need it to know when it sees: "g#2f4f4f;uitar" Any quick way of doing this? All help appreciated.

Upvotes: 2

Views: 36

Answers (1)

rassar
rassar

Reputation: 5660

Try using regular expressions (good site here)

def match_with_noise(word, noisy_word):
    return re.match("(.*)".join(word), noisy_word)

This returns a re.match object that is easy to deal with:

>>> match_with_noise("guitar", "g0923874uitar")
<_sre.SRE_Match object; span=(0, 13), match='g0923874uitar'>

For example, use .groups() to get the stuff that isn't supposed to be there:

>>> match_with_noise("guitar", "g0923874uitar").groups()
('0923874', '', '', '', '')

Upvotes: 2

Related Questions