Hi,
I've never used exception before, but I think now it's time to start.
I've seen that there is a list of the built-in exceptions in the
Python docs, but this explains the meaning of every exception. Does
exist an inverted list? I mean, how may I know what kind of exception
is going to raise my code? Shall I figure out by myself?
Apart from this, in a method I check valid strings with a regular
expression. I pass the string to the method and then Ii have something
like:
m = re.match('[1-9]$', my_string)
I was thinking to put a try except here, so that:
try:
m = re.match('[1-9]$', my_string)
except:
print 'something...'
But actually this doesn't work, because re.match just assign None to
m, without raising anything.
The only way seems to check with m.group(), so after I matched the
string I should have:
try:
m.group()
except:
print 'error...'
Or shall I put also the matching inside the try? Or am I completely wrong? 1 4812
Mr.SpOOn <mr********@gma il.comwrites:
Hi,
I've never used exception before, but I think now it's time to start.
I've seen that there is a list of the built-in exceptions in the
Python docs, but this explains the meaning of every exception. Does
exist an inverted list? I mean, how may I know what kind of exception
is going to raise my code? Shall I figure out by myself?
The interactive prompt will help you!
E.g. I want to know what exception arises when I try to add an int to a
string:
>>42 + 'spam'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
Ah, it's a TypeError!
Apart from this, in a method I check valid strings with a regular
expression. I pass the string to the method and then Ii have something
like:
m = re.match('[1-9]$', my_string)
I was thinking to put a try except here, so that:
try:
m = re.match('[1-9]$', my_string)
except:
print 'something...'
But actually this doesn't work, because re.match just assign None to
m, without raising anything.
The only way seems to check with m.group(), so after I matched the
string I should have:
try:
m.group()
except:
print 'error...'
Or shall I put also the matching inside the try? Or am I completely wrong?
In this case re.match has been designed to return None if there are no
matches so there is no need to look for exceptions. This is probably
because it is very common to use re.match(...) while expecting no match
so it is not really an 'exception'. Just do
m = re.match(...)
if m is None:
print 'error...'
else:
# Do something with m
HTH
--
Arnaud This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ron Adam |
last post by:
Is it possible to match a string to regular expression pattern instead
of the other way around?
For example, instead of finding a match within a string, I want to
find out, (pass or fail), if a string is a partial match to an re.
Given an re of 'abcd and a bunch of other stuff'
This is what i'm looking for:
|
by: greenflame |
last post by:
I am trying to find a regular expression that returns true in the
following cases but no others.
2.0
2.4
2.
324.0e345
234e34
34.e-43
234.673
|
by: hillcountry74 |
last post by:
Hi,
I'm stuck with this regular expression from past 2 days. Desperately
need help.
I need a regular expression that will allow all characters except these
*:~<>'
This is my code in VB.Net-
Dim regex As System.Text.RegularExpressions.Regex
|
by: Q. John Chen |
last post by:
I have Vidation Controls
First One: Simple exluce certain special characters:
say no a or b or c in the string:
*
Second One:
I required date be entered in "MM/DD/YYYY" format:
//+4 How ??
|
by: Engineerik |
last post by:
According to what I see in the manuals I should be able to use regular
expressions with the rowfilter property of a dataview but I keep getting
errors.
Example:
I want to find any rowview where column APCode is not a string of exactly 3
uppercase letters.
Assume dtBAirpt is an instance of a datatable having a column APCode
Code snippet
Try
Dim dvTest As New DataView(dtBAirpt, _
| |
by: Billa |
last post by:
Hi,
I am replaceing a big string using different regular expressions (see
some example at the end of the message). The problem is whenever I
apply a "replace" it makes a new copy of string and I want to avoid
that. My question here is if there is a way to pass either a memory
stream or array of "find", "replace" expressions or any other way to
avoid multiple copies of a string.
Any help will be highly appreciated
|
by: Mike |
last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in
matches. I would like to get what the actual regular expression is.
In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION
DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH
CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How
do I gain access to the expression (not the matches) at runtime?
Thanks,
Mike
|
by: PJ6 |
last post by:
Is it possible for a regular expression to macth only an empty string? What
I want is exactly {0}, but that causes the parser to throw an exception -
parsing "{0}" - Quantifier {x,y} following nothing.
Duh, I want nothing. Maybe I need an expression that evaluates to "not
anything?" I can't find that either.
Paul
|
by: DomoChan |
last post by:
When I attempt to name a group in a regular expression under TR1, the
library throws a non descriptive error "regular expression error".
The numbered reference group works, as in /1 to reference the first
group. However, any attempt to use (?<myGroup>expression) fails?
Does anyone have any insight into this?
Thanks!
-Velik
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |