I only need to print out the non-match item which "333-33". When I run the following code, it prints all the items such as "111-11", "222-22", "333-33"...
"333-33" should return the None type...
Any suggestions? Thank you very much in advance.
ALLOW_LIST = ["(4[0-4]{2})-\d{2}", "111-11", "222-22"]
item=[]
codes = ["111-11", "222-22", "333-33"]
for code in codes:
for i in ALLOW_LIST:
if re.search(i, code) is None:
item.append(code)
print(item)
You can use the all
function with a generator expression that iterates through ALLOW_LIST
to ensure that none of the patterns matches the current code
before outputting it:
[code for code in codes if all(not re.search(i, code) for i in ALLOW_LIST)]
From what I understand you want to include only items matched by allow list.
Your if statement will append items that are not matched : if re.search(i, code) is None:
You should edit out your if statement to include items that are matched:
ALLOW_LIST = ["(4[0-4]{2})-\d{2}", "111-11", "222-22"]
item=[]
codes = ["111-11", "222-22", "333-33"]
for code in codes:
for i in ALLOW_LIST:
if re.search(i, code):
item.append(code)
print(item)
You could form a single regex alternation here:
ALLOW_LIST = ["4[0-4]{2}-\d{2}", "111-11", "222-22"]
regex = r'^(?:' + '|'.join(ALLOW_LIST) + r')$'
items = []
codes = ["111-11", "222-22", "333-33"]
for code in codes:
if not re.search(regex, code):
items.append(code)
print(items) # ['333-33']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.