Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理字符串匹配的Python_Python_Regex_String Matching - Fatal编程技术网

处理字符串匹配的Python

处理字符串匹配的Python,python,regex,string-matching,Python,Regex,String Matching,有这样一个字符串: mystr = 'account_id 37318 not found' 我想知道如何写出比以下更好的条件: if 'account_id' not in str and 'not found' not in str: doSomething() 我想一定有这样的事情: if 'account_id' + %any substring% + 'not found' not in str: doSomething() 也许正则表达式可能会有所帮助,但我不擅长

有这样一个字符串:

mystr = 'account_id 37318 not found'
我想知道如何写出比以下更好的条件:

if 'account_id' not in str and 'not found' not in str:
    doSomething()
我想一定有这样的事情:

if 'account_id' + %any substring% + 'not found' not in str:
   doSomething()
也许正则表达式可能会有所帮助,但我不擅长使用它

提前感谢。

您可以使用或不使用内置关键字作为变量名

if all(i not in s for i in ('not found', 'account_id')):
例如:

>>> tr = 'account_id 37318 not found'
>>> tr1 = '2735723'
>>> all(i not in tr for i in ('not found', 'account_id'))
False
>>> all(i not in tr1 for i in ('not found', 'account_id'))
True
>>>
您可以使用或不使用内置关键字作为变量名

if all(i not in s for i in ('not found', 'account_id')):
例如:

>>> tr = 'account_id 37318 not found'
>>> tr1 = '2735723'
>>> all(i not in tr for i in ('not found', 'account_id'))
False
>>> all(i not in tr1 for i in ('not found', 'account_id'))
True
>>>
这可能会有所帮助

import re
string = 'account_id 37318 not found'

match = re.search(r'\baccount_id\b.*?\bnot found\b',string)
if match:
    print 'Do something'
else:
    print 'Do nothing'
如果有帮助,请告诉我:)。

这可能会有帮助

import re
string = 'account_id 37318 not found'

match = re.search(r'\baccount_id\b.*?\bnot found\b',string)
if match:
    print 'Do something'
else:
    print 'Do nothing'

如果有帮助,请告诉我:)。

首先
str
是一个非常糟糕的变量名。哦,是的,当然)这只是一个例子。我会先
str
是一个非常糟糕的变量名。哦,是的,当然)这只是一个例子。我可能最好使用
\baccount\u id\b.*?\b未找到\b
(不需要描述字符串的开头和结尾,使用文本字符串启动模式总是更有效)。可能最好使用
\baccount\u id\b.*?\b未找到\b
(不需要描述字符串的开始和结束,使用文本字符串启动模式总是更有效)。