Python negative lookahead for aaa:bbb@hostname

Hi, below python regular expression is for getting id/pwd/hostname/port from user input.

Python Re:

Target String:

Could you please explain above bold formatted characters' roles ?
I am not sure why (:?) has parenthesis however :? before "port" parsing does not have one.
I am not sure why (?!\\) was used why do we need negative look-ahead for '\' ? It works without (?!\\) regex.

Regular Expression tool:
Code used:
EK 365Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
:? means: 0 or more ":"

The first (:?) is the capturing group 2. The second :? is not in a capturing group.

The only reason I can think of for (?!\\) is Windows UNC paths, that contain \.

So the regular expression will stop if that path is of the type: user:pass\\hostname

EK 365Author Commented:
Thank you but i tried with windows unc with/without negative look-ahead but same result it's not working w that re string.
Dan CraciunIT ConsultantCommented:
Reading the code and the comments (you know, when nothing works, read the manual):
>>Note that if your password has the '@' character then you must backslash escape it.

So the (?!\\) is there so this works:

(?P<password>.*)(?!\\)@: match until there are no more \ characters, then stop before the last "@".

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
:? means: 0 or more ":"
No, it means zero or one.

The negative lookahead is pointless. The very next character to be found is an @, so the lookahead will always be true.
kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
Selected answer is not correct. If the lookahead were instead a lookbehind, then Dan Craciun would be correct.

You can see why using the following string:


By Dan's (and the referenced article's logic), the password should be "p", but the result is "p@ssword4\\@host". As I said, the lookahead is pointless in this scenario.
EK 365Author Commented:
thank you! I reached out author of that code and he said the same thing!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.