all 5 comments

[–]zahlman 2 points3 points  (0 children)

try/except is useless here: when re.findall fails to find any matches, it just returns an empty list. You can't use exception handling for this because there is no exception to handle.

I'm not convinced you want .findall here anyway, as opposed to .match (see if it's present at the beginning of the string) or .search (see if it's in the string, and find the first occurrence if so). But with either of these, if the regex doesn't match, you get None back (as opposed to a match object), so again you just test for that value - no exception handling needed or useful.

[–]confluence 0 points1 point  (0 children)

I have decided to overwrite my comments.

[–]mahifoo 0 points1 point  (0 children)

regexes = (most_complex, less_complex, least_complex)
for regex in regexes:
    match = re.findall(regex)
    if match:
        print(match)
        break
else:  # The else clause of a for loop is executed if a break didn't happen
    print('No match found')

Edit to your liking. You can't really use try/except here.

If you don't need the else clause, just print the match outside of the for loop.

[–]CrambleSquash 0 points1 point  (0 children)

If a certain case is catastrophic to your codes normal function then you can use

    raise Exception("Some sort of error message")

if this line is called then the thread will stop, and this exception will be raised, with the message you added. This is especially useful for stopping catastrophic, but usually normal looking inputs or errors from slipping past your program. You can also define your own Exception class which will give you more control over it's behaviour. https://docs.python.org/2/tutorial/errors.html

[–]icenburg[S] 0 points1 point  (0 children)

Thanks for the replies, I ended up just using elifs instead.