Python negative lookahead for aaa:bbb@hostname

EK 365
EK 365 used Ask the Experts™
on
Hi, below python regular expression is for getting id/pwd/hostname/port from user input.

Python Re:
(?P<username>[^@:]*)(:?)(?P<password>.*)(?!\\)@(?P<hostname>[^:]*):?(?P<port>[0-9]*)

Target String:
aaa:bbb@hostname:22

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: http://pythex.org/
Code used: https://github.com/pexpect/pexpect/blob/master/examples/hive.py
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
:? 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

HTH,
Dan

Author

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.
Commented:
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:
user:p\@ssword4@host2:11

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

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

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.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

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:

user:p@ssword4\@host@2:11

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.

Author

Commented:
thank you! I reached out author of that code and he said the same thing!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial