Avatar of EK 365
EK 365

asked on 

Python negative lookahead for aaa:bbb@hostname

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
Regular ExpressionsPython

Avatar of undefined
Last Comment
EK 365

8/22/2022 - Mon