problem: throwing exception in re.match function
I have one simple question about throwing exeptions in re.match function
Is there a way to 'match' function threw an exception? Maybe some flag. If this is not possible maybe there is some other function in RE library which throw an exception when the list will be empty for example.
This example in some cases returns None
I am looking for a method that would throw Exception, not None I can do this by 'if' statement, but mayby there is any other way
There is no such function(s) in Python re module. See the doc:
Exception raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching. It is never an error if a string contains no match for a pattern.
if re.match(".*"+str(country).lower()+"."+str(city).lower()+".*", link) is None: raise Exception('whatever')
Erm? Exceptions should happen only in exceptional situation, not the one you are describing. But, if you have to, you can always check return value using if and then raise exception.
Edit: and even make own function, to hide the exception throwing...
If you want to throw exception??? so you can use index string method, for example:
verify_link = link.index(str(country).lower()+"."+str(city).lower())
EDITED: You should take into account that '.' will match dot only in my example above.
But i think that this is not the best way to check that sub-string is within string.