Python字符串匹配

Raj*_*eev 14 python regex string

如果包含字符串*SUBJECT123,如何subject在python中确定字符串中包含该字符串?

gho*_*g74 34

if "subject" in mystring.lower():
  # do something
Run Code Online (Sandbox Code Playgroud)


Fel*_*ing 12

如果你想要subject匹配SUBJECT,你可以使用re

import re
if re.search('subject', your_string, re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)

或者您可以先将字符串转换为小写,然后使用:

if "subject" in your_string.lower()
Run Code Online (Sandbox Code Playgroud)


Ank*_*wal 7

另一种方式

mystring.find("subject")
Run Code Online (Sandbox Code Playgroud)

如果字符串包含"subject",则上述答案将返回true,否则返回false.而如果存在,则find将返回其在字符串中的位置,否则为负数.