Thursday 15 September 2011

regex - Bulk replace with regular expressions in Python -


For a Django application, I need to convert all events into a single string in a string in a link if I have Resource is related to match in my database

Now, this process is: - I use re.sub to process a very long string of text - when re.sub gets a pattern matching, it runs a function It shows whether the pattern matches an entry in that database - if there is a match, then it wraps the link, wrapping the link around the match.

The problem is that sometimes there are hundreds of hits on the database. What I want to be able to do is a single bulk query for the database.

So: Can you do a bulk search and use regular expressions in Python?

The context, here is the code (for the curious, the patterns I see are for legal quotes):

  def add_linked_citations (text): linked_text = re .sub (? 'P' (? P & lt; Volume & gt; [0-9] + [a-zA-Z] {0.3}) \ s + (P & LT;; Correspondent & gt; [AZ] {1 , [49] [a-zA-Z0-9 \ s] , Create_citation_link, text) Return LinkedText def create_citation_link (match_object): volume = none reporter = if there is no match_object.group ("reporter") not in [none, ']] then no page = none if match_object.group ("Clause") [none, ' ']: Amount = match_object is not in the group ("Quantity"): If match_object.group ("Page") [None,' ']: page = match_object.group ("Page") is not in Reporter = match_object.group ("Reporter") Volume and Reporter and Pages: # These will all be here ... # !!! Here's where I hit the database quotes = quotation = expired.filters (volume = volume, reporter = reporter, page = page) if quotes Exists (): quotation = extract [0] document = citation.document url = document.url () return '& lt; A href = "% s" & gt;% s% s% s & lt; / A & gt; Other (%, URL, Volume, Reporter, Page) Other: Returns '% s% s% s'% (Volume, Reporter, Page)    

No comments:

Post a Comment