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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:41:42+00:00 2026-05-29T09:41:42+00:00

I have a Python regex that takes a string (database connection URI) and splits

  • 0

I have a Python regex that takes a string (database connection URI) and splits it using named groups into username, password etc.

uri = 'username:password@host/database'
m = re.compile('^(?P<user>[^:@]+)(\:(?P<password>[^@]*))?@(?P<host>[^\:@/]+)(\:(?P<port>[0-9]+))?/(?P<db>[^\?]+)?$').match(uri)
print m.groupdict()
{'host': 'host', 'password': 'password', 'db': 'database', 'user': 'username', 'port': None}

This works fine. The problem is if the uri has a @ symbol in it, since that’s used to split password and host. For example,

uri = 'username:p@ssword@host/database'

will not match, which is expected. However, I’d like to be able to escape the special character, eg:

uri = 'username:p\@ssword@host/database'

and have it match. My regex experience is pretty limited – I guess what I’d like to do is modify the

(?P<password>[^@]*)

group so that it will match any character that’s not a @, unless it’s preceded by a \ character. Of course, some (most) connection strings will not contain a \@ at all.

Any help much appreciated.

  • 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-29T09:41:43+00:00Added an answer on May 29, 2026 at 9:41 am

    My take is you want greedy matching, that is password is up the last @ and hostname is between last @ and first /

    A simple way could be like this:

    In [68]: re.match('((?P<user>.*):)((?P<pass>.*)@)((?P<host>.*)/)((?P<db>.*))', "username:p@ssword@host/data").groupdict()
    Out[68]: {'db': 'data', 'host': 'host', 'pass': 'p@ssword', 'user': 'username'}
    

    You might want to add optionals, that is (stuff)+ if e.g. username and password can be omitted.

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

Sidebar

Related Questions

In Python compiled regex patterns have a findall method that does the following: Return
I have a Python 3 script that is going to be doing some regex
I have a Python script that we're using to parse CSV files with user-entered
I have a string on which I try to create a regex mask that
Using a regex in Python, how can I verify that a user's password is:
Currently, I have the following Python regex: r'^https?://(www.)?domain.com/?(?P<path>.*)/?$' That I'm replacing with: r'/\g<path>/' This
I have been using python with regex to clean up a text file. I
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or
I have a Python script that takes in '.html' files removes stop words and
Good Early Morning, I have the following python regex file that we established on

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.