0

I have a list of multiple items. Need to create a loop that finds an item from the list and prints it out. If item not found prints out only once that it hasn't been found.

 for x in range(len(regs)):
 rex = regs[x]
 if re.match(rex,hostname):
 print (dev[x],'Regex matched')
 break
 else:
 print('wrong format')

Currently it prints out multiple times that it hasn't been found. When it matches the regex it prints out at the end. I only want the If Else to prints "wrong format" once.

currently prints out wrong format wrong format wrong format wrong format wrong format wrong format wrong format "DeviceX Regex matched"

asked Sep 13, 2019 at 10:29
1
  • a simple solution will be to use a flag Commented Sep 13, 2019 at 10:32

5 Answers 5

1

Use else with for.

As you are breaking out of the for loop as soon as you find one item, the below code will work fine:

for x in range(len(regs)):
 rex = regs[x]
 if re.match(rex,hostname):
 print (dev[x],'Regex matched')
 break
else:
 # Does not execute only when a break statement
 # is executed inside the for loop.
 print('wrong format')
answered Sep 13, 2019 at 10:35
Sign up to request clarification or add additional context in comments.

2 Comments

just a gentle reminder.. it's not a good practice to use for-else construct but in this case its fine I guess
Agreed! But in this short and simple code, I think it is fine.
0

It is printed multiple times, because there are multiple times where the current value does not match. A simple way to fix this is to use a flag, like so:

value_found = False 
for x in range(len(regs)):
 rex = regs[x]
 if re.match(rex,hostname):
 print (dev[x],'Regex matched')
 value_found = True
 break
if not value_found:
 print('wrong_format')
answered Sep 13, 2019 at 10:34

Comments

0

You can maintain a flag variable as follows.

matched = False
for x in range(len(regs)):
 rex = regs[x]
 if re.match(rex, hostname):
 print(dev[x], "Regex matched!")
 matched = True
 break
if not matched:
 print("Wrong format")
answered Sep 13, 2019 at 10:35

Comments

0

I think you can make it shorter using comprehensive list such as:

if len([r for r in regs if re.match(r, hostname)]) == 1:
 print(dev[regs.index([r for r in regs if re.match(r, hostname)][0])], 'Regex matched')
else:
 print('not found')
answered Sep 13, 2019 at 10:44

1 Comment

This code does not work if multiple elements match. The first condition should be len(...) >= 1. And you would probably also need to change the statement in the print.
0

This should solve your problem.

flag = 0 
for x in range(len(regs)):
 rex = regs[x]
 if re.match(rex, hostname):
 print(dev[x], 'Regex matched')
 flag = 1
 break
if flag == 0:
 print('wrong format')
Chinni
1,2901 gold badge16 silver badges28 bronze badges
answered Sep 13, 2019 at 10:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.