1

How can I improve this regex (?<=\/\/)(.*?)(?=\@) to extract the username:password from a postgresql connection string.

I want to extract this string db-admin-username@server:this-is-funny-passowrd/is-continuing= from this string postgresql://db-admin-username@server:this-is-funny-passowrd/[email protected]/dbname

It returns the wrong string in case the password or username contains @

It should extract anything between // and the last @ characters.

3
  • Did you try substring('postgresql://db-admin-username@server:this-is-funny-passowrd/[email protected]/dbname' from '//(.*)@')? Commented Dec 15, 2020 at 14:02
  • Or SELECT regexp_matches(col, '//(.*)@') Commented Dec 15, 2020 at 14:04
  • Thank you! @WiktorStribiżew, using this regex ` '//(.*)@'` returns the required string. Commented Dec 15, 2020 at 14:48

1 Answer 1

1

You can use

//(.*)@

In Python, you can use

re.findall(r'//(.*)@', text)

The .* will match from the leftmost // till the rightmost @, thus returning all the string you need.

Sign up to request clarification or add additional context in comments.

3 Comments

I wasn't particularly selecting from a database but the regex '//(.*)@' finds a match in a string.
@N.Solomon So, what should I keep in the answer? If it works for you as is please consider accepting the answer. Else, let me know what to fix.
I only needed the regex //(.*)@ All I had to do in python is matches = re.findall(r'\/\/(.*)@',conn_string) or in Js ` connString.match(/\/\/(.*)@/g);`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.