Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:12:54+00:00 2026-05-25T14:12:54+00:00

Friends: in PostgreSQL plpython, am trying to do an iterative search/replace in a text

  • 0

Friends: in PostgreSQL plpython, am trying to do an iterative search/replace in a text block ‘data’.

Using re-sub to define a match pattern, then call a function ‘replace’ to do the work.
Objective is to have the ‘replace’ function called repeatedly, as some replacements generate further ‘rule’ matches, which require further replacements.

All works well through many, many replacements – and I’m managing to trigger the 2nd Pass of the repeat loop. Then, until something causes the Regex pattern to return an integer(?) — apparently at the point it finds no matches… ?? I’ve tried testing for ‘None’ and ‘0’, with no luck. Ideas?

data = (a_huge_block of_text)

# ======================  THE FUNCTION  ==============
def replace(matchobj):
 tag = matchobj.group(1)
 plpy.info("-------- matchobj.group(1), tag: ", tag)
 if matchobj.group(1) != '':
  (do all the replacement work in here)
# ======================  END FUNCTION  ==============

passnumber = 0
# If _any_ pattern match is found, process all of data for _all_ matches:
while re.search('(rule:[A-Za-z#]+)', data) != '':
 # BEGIN repeat loop:
 passnumber = passnumber + 1
 plpy.info(' ================================  BEGIN PASS: ',  passnumber)

 data = re.sub('(rule:[A-Za-z#]+)', replace, data)
 plpy.info(' =================================== END PASS: ',  passnumber)

Above code seems to be running OK, into a second iteration… then:

ERROR:  TypeError: sequence item 21: expected string, int found
CONTEXT:  Traceback (most recent call last):
  PL/Python function "myfunction", line 201, in <module>
    data = re.sub('(rule:[A-Za-z#]+)', replace, data)
  PL/Python function "myfunction", line 150, in sub
PL/Python function "myfunction"

Have also tried re.search (…) != ” — and re.search (…) != ‘None’ — with same result.
I do realize I must find the syntax to represent the match object in some readable form…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:12:55+00:00Added an answer on May 25, 2026 at 2:12 pm

    The answer to this turned out to be quite simple, of course, once you know Python! (I don’t!)

    To initiate the repeat loop, I had been doing this test:

    while re.search('(rule:[A-Za-z#]+)', data) != '':
    

    Had also tried this one, which will also not work:

    while re.search('(rule:[A-Za-z#]+)', data) != 'None':
    

    The None result can be trapped, of course, but the quotes are not needed. It’s as simple as that:

    while re.search('(rule:[A-Za-z#]+)', data) != None:
    

    It’s all so simple, once you know!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an ASP.NET AJAX framework other than just using UpdatePanel & Friends or
I am developing a website using CodeIgniter and PHP. Some of my friends suggest
Friends In our C++ , Iam current using realloc method to resize the memory
friends, i want to set layout_marginBottom using java code or dynamically in list view
friends, right now i am showing list of map overlays on google map using
hai Friends I am using this coding for validating the emailID online through webservice
Hai Friends I am using webservice for validating the Email in the Online Its
hai friends I am using a file upload control to upload the files if
friends, i am using following code to get GPS location it is causing two
friends, i am using following code to display list with radio buttons now i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.