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 6167241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:28:35+00:00 2026-05-23T22:28:35+00:00

The following query takes out the parentheses from a string, i.e. it regex replaces

  • 0

The following query takes out the parentheses from a string, i.e. it regex replaces them with nothing. It works as expected when I test it in pgAdimin III (1.12), but when part of a python script using psycopg2, it does not replace the parentheses at all.

 SELECT
    regexp_replace(location.name, '\\(|\\)', '', 'g') AS host
 FROM
    location

I’m running python 2.7.1 with psycopg2 2.3.2 and my OS is SLES 11 SP1.

I expect that a postgres query run in pgAdmin would return the same exact results as one ran with psycopg2, or is that an incorrect assumption? I can provide data if needed, but location.name is a string, e.g.

(goat) 172.10.x.x -> /var/log/messages

EDIT: Python code:

cursor.execute("""
     SELECT
        regexp_replace(location.name, '\\(|\\)', '', 'g') AS host
     FROM
        location
""") 

The parameterized arguments looks like the answer.

  • 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-23T22:28:35+00:00Added an answer on May 23, 2026 at 10:28 pm

    Use parametrized arguments:

    sql='SELECT regexp_replace(location, %s, %s, %s)  from foo'
    cursor.execute(sql,[r'\(|\)','','g'])
    

    For example:

    import psycopg2
    connection=psycopg2.connect(
        database=config.PGDB,
        host=config.HOST,
        password=config.PASS)
    cursor=connection.cursor()
    sql='CREATE TABLE foo (location varchar(40))'
    cursor.execute(sql)
    sql='INSERT INTO foo (location) VALUES (%s)'
    cursor.execute(sql,['(goat) 172.10.x.x -> /var/log/messages'])
    sql='SELECT * FROM foo'
    cursor.execute(sql)
    data=cursor.fetchall()
    print(data)
    # [('(goat) 172.10.x.x -> /var/log/messages',)]
    
    sql='SELECT regexp_replace(location, %s, %s, %s) FROM foo'
    cursor.execute(sql,[r'\(|\)','','g'])
    data=cursor.fetchall()
    print(data)
    # [('goat 172.10.x.x -> /var/log/messages',)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following query takes more than 3 minutes to run because tables contain massive
The following query takes FOREVER to execute (30+ hrs on a Macbook w/4gig ram)
The following simple query takes a very long time (several minutes) to execute. I
I have an SQL query that takes the following form: UPDATE foo SET flag=true
Take the following query: select * from ( select a, b from c UNION
I'm stuck trying to remove the nested part from the following query : SELECT
I have the following MySql query: select t1.* from Table1 t1 inner join Table2
I am trying to translate the following query into entity framework: SELECT Subjects.Name FROM
I have the following query: EXPLAIN EXTENDED SELECT * FROM ( `photo_data` ) LEFT
The following query is in SQL: SELECT r.reservation_no, f.flight_no, r.departure_date, f.departure_time, f.duration FROM flights

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.