troubleshooting Question

Python negative lookahead for aaa:bbb@hostname

Avatar of EK 365
EK 365 asked on
Regular ExpressionsPython
6 Comments1 Solution327 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Dan Craciun
IT Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros