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.

Example:

verify_link=re.match(".*"+str(country).lower()+"."+str(city).lower()+".*",link)

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

Answers


There is no such function(s) in Python re module. See the doc:

exception re.error

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.


Need Your Help

Magento, precomplete custom options while creating/editing a product in admin

magento admin product options catalog

I’m trying to precomplete custom options during the process of creating or editing a product in the Magento admin : after product type choice.

How Scalable is Codeigniter V Other PHP Frameworks?

php codeigniter cakephp kohana

How Scalable is Codeigniter V Other PHP Frameworks? I'm new to Codeigniter and love to know how scalable it is when compared to other PHP Frameworks?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.