在我的代码中,我使用了四个正则表达式条件,但它太长了,所以有简单或通用的方法可以组合这些条件吗?
s_l = ['abcabcabcabc', 'defdef.', 'sssssss', 'def', 'def,', 'xyzabc,', 'efgdefefg']
for idx, str_item in enumerate(s_l):
if (re.match(r'(abc|def)+', str_item) # find abc or def in str
and re.match(r'^((?!(xyz|efg)).)*$', str_item) # find xyz and efg not in str
and re.match(r'^.*[^,|.]$', str_item) # comma not in the end of str
and re.match(r'^[a-zA-Z]{1,10}$', str_item)): # find length of str smaller than 10
print(idx, "True")
else:
print(idx, "False")