Does re.compile() or any given Python library call throw an exception?

I can't tell from the Python documentation whether the re.compile(x) function may throw an exception (assuming you pass in a string). I imagine there is something that could be considered an invalid regular expression. The larger question is, where do I go to find if a given Python library call may throw exception(s) and what those are?

Asked by: Kristian510 | Posted: 28-01-2022

Answer 1

Well, re.compile certainly may:

>>> import re
>>> re.compile('he(lo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python25\lib\", line 180, in compile
    return _compile(pattern, flags)
  File "C:\Python25\lib\", line 233, in _compile
    raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis

The documentation does support this, in a roundabout way - check the bottom of the "Module Contents" page for (brief) description of the error exception.

Unfortunately, I don't have any answer to the general question. I suppose the documentation for the various modules varies in quality and thoroughness. If there were particular modules you were interested in, you might be able to decompile them (if written in Python) or even look at the source, if they're in the standard library.

Answered by: Ted853 | Posted: 01-03-2022

Answer 2

Unlike Java, where there are exceptions that must be declared to be raised (and some that don't have to be, but that's another story), any Python code may raise any exception at any time.

There are a list of built-in exceptions, which generally has some description of when these exceptions might be raised. Its up to you as to how much exception handling you will do, and if you will handle stuff gracefully, or just fail with a traceback.

Answered by: Sawyer511 | Posted: 01-03-2022

Similar questions

python - How to print out the line after the line found in re.compile()

Using this code import re file = open('FilePath/OUTPUT.01') lines = file.close() for match in re.finditer(r"(?m)^\s*-+\s+\S+\s+(\S+)", lines): eng = open('Tmp.txt', 'w').writelines(eng) print I get a column of data that looks like this: -1.1266E+05 -1.1265E+05 -1.1265E+05 -1.1265E+05 -1.1264E+05 ...

Python regex re.compile() match string

I am trying to grab the version number from a string via python regex... Given filename: facter-1.6.2.tar.gz When, inside the loop: import re version = re.split('(.*\d\.\d\.\d)',sfile) print version How do I get the 1.6.2 bit into version

python - Matching general integer ranges with re.compile()

How do I target integer ranges with re.compile() where the limits of the range are two general integers a and b? For example, say I want to target strings like: foo_bar_8 foo_bar_12 i.e. the number at the end is within the range 8-14 (a=8, b=14

python - Shouldn't this re.compile() expression find all links in a page?

I have a lot of difficulty understanding the re.compile() method in python. I found this example and from what I read it should find all links in a webpage. Is this correct? Can someone explain the "(.*?) part of the expression? It's not making much sense to me, and it doesn't seem to be finding the links in a page. link_finder = re.compile('href="(.*?)"') links = link_finder.findall(html) ...

python - Unable to use re.compile()

This program is a very simple example of webscraping. The programs goal is to go on the internet, find a specific stock, and then tell the user the price that the stock is currently trading at. However, I run into the issue in the code that when I compile it, this error message comes up: Traceback (most recent call last): File "", line 15, in &lt;module&gt; ...

regex - re.compile() doesn't match the valid pattern against a string python

This question already has answers here:

regex - python re.compile() and re.findall()

So I try to print only the month, and when I use : regex = r'([a-z]+) \d+' re.findall(regex, 'june 15') And it prints : june But when I try to do the same for a list like this : regex = re.compile(r'([a-z]+) \d+') l = ['june 15', 'march 10', 'july 4'] filter(regex.findall, l) it prints the same list like they didn't take in count the fact that I don't want...

regex - how to do re.compile() with a list in python

I have a list of strings in which I want to filter for strings that contains keywords. I want to do something like: fruit = re.compile('apple', 'banana', 'peach', 'plum', 'pinepple', 'kiwi'] so I can then use, list_of_strings) to get only the strings containing fruits, but I'm not sure how to use a list with re.compile. Any suggestions? (I'm not set on using

python - Search using dictionary with re.compile()

I ran into a problem while creating a parser. The line I want to parse in a txt file looks like this: '&gt;NAME= (various_names_here)' Every time I see this "&gt;Name=" I want to read in the line. My dictionary has an item: my_dict = { 'name': re.compile(r'&gt;NAME= (?P&lt;name&gt;.*)\n'), } Reading my txt file in by line with a

python - Is there a way to OR several re.compile() outputs?

Suppose that I already have some objects created by re.compile(). x = re.compile('abc') y = re.compile('abd') Is there a way to OR x and y into a new object? z = re.compile('abc|abd') Note that I used the original regexes "abc" and "abd". But in real situations, I may not know them, I only know x and y. I woul...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python