Rewrite regex without negation

放肆的年华 提交于 2019-12-07 22:20:25

问题


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.


回答1:


Try this regex:

https?:[^:]*$

Regex live here.




回答2:


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?:\/(?:[^\/]|$)))*$


来源:https://stackoverflow.com/questions/31832852/rewrite-regex-without-negation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!