【问题标题】:Find a word inbetween other letters Python在其他字母之间查找一个单词 Python
【发布时间】:2017-12-14 11:52:43
【问题描述】:

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

【问题讨论】:

  • 使用正则表达式并删除特殊字符和数字。但是,你也会有 gfffuitar 并且它不会匹配吉他。
  • #2f4f4f; 看起来像通常在 html 或 css 源中看到的颜色代码。 #...; 模式。

标签: string python-3.x artificial-intelligence


【解决方案1】:

尝试使用正则表达式 (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', '', '', '', '')

【讨论】:

  • 不是一个完整的答案,但绝对有用的信息。我应该能够使用它并消除它所产生的噪音。干杯!
猜你喜欢
  • 2015-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 2016-03-09
  • 2017-06-18
  • 2016-01-26
相关资源
最近更新 更多