Python: How do you use re to ignore links in parentheses?
Question: The relevant part of the code is:

    
    
    import re
    reargs = '<a\s*href=[\'|"](.*?)[\'"].*?>'
    link = re.search(reargs,content,flags=re.IGNORECASE)
    

I'm building a crawler and the web pages I'm working with have links in
parentheses that I don't want so it would be like:

Foo foo foo foo (_link_) foo foo foo foo _link_ foo foo foo foo (foo _link_
foo) foo foo _link_ foo foo _link_......and so on


Answer: If there can be multiple sets of nested parentheses like "((foo) _link_)", I
don't think this is possible with regular expressions. In particular, note
that parentheses can be used inside URLs (such as at
[wikipedia](http://en.wikipedia.org/wiki/Python_%28programming_language%29)),
so there may still be nested parens even if the text itself doesn't contain
any. So, in the general case I don't think this can be done with regex.

In order to solve it, I will assume you can have parentheses at most 1 level
deep, and that no URLs contain parentheses.

The regex you're looking for is something like the following:

    
    
    (\([^\)]*\)|[^\(<])*_link_
    

Where `_link_` is a regular expression matching a link (which you describe in
the problem statement, though it might need some tweaking). To summarize what
that first part of my regex is: it matches 0 or more of either a parenthetical
statement or a non-link non-parenthesis character. Now, use the matched back
references (`link.group(2)` in your example) to find your URL.

