【问题标题】:How to find a string inside of a string space insensitive? [duplicate]如何在字符串空间不敏感中找到字符串? [复制]
【发布时间】:2018-06-05 20:42:18
【问题描述】:

例如,如果一个字符串包含:

odfsdlkfn dskfThe Moonaosjfsl dflkfn

如何检查它是否包含“月亮”?

我目前正在做的是(但不起作用):

if string.find("The Moon")!=-1:
    doSomething

有没有办法做到这一点?

谢谢!

【问题讨论】:

  • "The Moon" in string?
  • 字符串中的“月亮”??
  • if 'The Moon' in string
  • @timgeb 也许你可以关闭它
  • @DevinGP 当你说(but does not work) 时,告诉我们它是如何不工作的。它应该工作

标签: python string python-3.x if-statement contains


【解决方案1】:

简单:

string = 'odfsdlkfn dskfThe Moonaosjfsl dflkfn'
if 'The Moon' in string:
    dosomething

【讨论】:

    【解决方案2】:

    你可以使用正则表达式:

    import re
    text = "odfsdlkfn dskfThe Moonaosjfsl dflkfn"
    if re.find("The Moon", text):
        ...
    

    在这种情况下,如果需要,您可以使用 re(pattern, text, re.IGNORECASE) 输入大小写。

    【讨论】:

    • 您可能还想解释什么时候正则表达式比简单的in/not in 检查更受欢迎...
    猜你喜欢
    • 1970-01-01
    • 2010-11-26
    • 2012-08-14
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多