Regex or other way to check for word in variable?
This question already has an answer here:
All you have to do is change your ordering. You can use the operator 'in' to check if a string contains another string.
if 'xyz' in 'oiurwernjfndj xyz iekjerk':
print True
You can probably just use in operator or str.find: https://docs.python.org/3/reference/expressions.html#in https://docs.python.org/2/library/stdtypes.html?highlight=index#str.find
Basically, you would do
if(xyz in post["body"] ...) {
// OR
if(post["body"].find('xyz') != -1 and ... ) {
The in operator is probably the fastest way.
The .find will return a number between 0 and string length -1 (to tell you the location of the string. However, if it cannot find the string, it returns a -1. So if it returns -1, then it isn't there, so anything other than -1 means it is.
链接地址: http://www.djcxy.com/p/9396.html上一篇: Python列表片语法没有明显的原因
下一篇: 正则表达式或其他方式来检查变量中的单词?