代码之家  ›  专栏  ›  技术社区  ›  Kermit

在其他字母Python之间查找一个单词

  •  2
  • Kermit  · 技术社区  · 7 年前

    我需要我的程序能够区分一个单词,即使它在一些字母之间有几个字符。 例如,假设它被赋予“吉他”。 我需要它知道它什么时候看到:“g#2f4f4f;uitar” 有什么快速的方法吗? 感谢所有帮助。

    1 回复  |  直到 7 年前
        1
  •  2
  •   rassar    7 年前

    尝试使用正则表达式( good site here )

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

    这将返回 re.match 易于处理的对象:

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

    例如,使用 .groups() 要获取不应该存在的东西:

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