0

My question is pretty similar to this question and the answer is almost fine. Only I need a regexp not only for character-to-character but for a second occurance of a character till a character. My purpose is to get password from uri, example:

http://mylogin:mypassword@mywebpage.com

So in fact I need space from the second ":" till "@".

Community
  • 1
  • 1
Leo
  • 2,061
  • 4
  • 30
  • 58

2 Answers2

3

You could give the following regex a go:

(?<=:)[^:]+?(?=@)

It matches any consecutive string not containing any : character, prefixed by a : and suffixed by a @.

Depending on your flavour of regex you might need something like:

:([^:]+?)@

Which doesn't use lookarounds, this includes the : and @ in the match, but the password will be in the first capturing group.

The ? makes it lazy in case there should be any @ characters in the actual url string, and as such it is optional. Please note that that this will match any character between : and @ even newlines and so on.

rvalvik
  • 1,559
  • 11
  • 15
0

Here's an easy one that does not need look-aheads or look-behinds:

.*:.*:([^@]+)@

Explanation:

  • .*:.*: matches everything up to (and including) the second colon (:)
  • ([^@]+) matches the longest possible series of non-@ characters
  • @ - matches the @ character.

If you run this regex, the first capturing group (the expression between parentheses) will contain the password.

Here it is in action: http://regex101.com/r/fT6rI0

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137