alexsmn
alexsmn

Reputation: 1746

Rewrite regex without negation

I have wrote this regex to help me extract some links from some text files:

https?:\/\/(?:.(?!https?:\/\/))+$

Because I am using golang/regexp lib, I'm not able to use it, due to my negation (?!..

What I would like to do with it, is to select all the text from the last occurance of http/https till the end.

sometextsometexhttp://websites.com/path/subpath/#query1sometexthttp://websites.com/path/subpath/#query2

=> Output: http://websites.com/path/subpath/#query2

Can anyone help me with a solution, I've spent several hours trying different ways of reproducing the same result with no success.

Upvotes: 2

Views: 120

Answers (2)

ndnenkov
ndnenkov

Reputation: 36110

The lookaheads exist for a reason.

However, if you insist on a supposedly equivalent alternative, a general strategy you can use is:

(?!xyz)

is somewhat equivalent to:

$|[^x]|x(?:[^y]|$)|xy(?:[^z]|$)

With that said, hopefully I didn't make any mistakes:

https?:\/\/(?:$|(?:[^h]|$)|(?:h(?:[^t]|$))|(?:ht(?:[^t]|$))|(?:htt(?:[^p]|$))|(?:http(?:[^s:]|$))|(?:https?(?:[^:]|$))|(?:https?:(?:[^\/]|$))|(?:https?:\/(?:[^\/]|$)))*$

Upvotes: 2

user4227915
user4227915

Reputation:

Try this regex:

https?:[^:]*$

Regex live here.

Upvotes: 3

Related Questions