Regex to match all URLs except certain URLs

后端 未结 1 479
不知归路
不知归路 2020-12-21 02:19

I need to match all valid URLs except:

http://www.w3.org
http://w3.org/foo
http://www.tempuri.org/foo

Generally,

相关标签:
1条回答
  • 2020-12-21 02:53

    The following regular expression:

    https?://(?!w3|tempuri)([-\w]*\.)(?!w3|tempuri)\S*
    

    only matches the first four lines from the following excerpt:

    https://ok1.url.com
    http://ok2.url.com
    https://not.ok.tempuri.com
    http://not-ok.either.w3.com
    
    http://no1.w3.org
    http://no2.w3.org
    http://tempuri.bla.com
    http://no4.tempuri.bla
    http://no3.tempuri.org
    http://w3.org/foo
    http://www.tempuri.org/foo
    

    I know what you're thinking, and the answer is that in order to match the above list and only return the first two lines you'd have to use the following regular expression:

    https?://(?!w3|tempuri)([-\w]*\.)(?!w3|tempuri)([-\w]*\.)(?!w3|tempuri)\S*
    

    which, in truth, is nothing more than a slight modification of the first regular expression, where the

    (?!w3|tempuri)([-\w]*\.)
    

    part appears twice in a row.

    The reason why your regular expression wasn't working was because when you include . inside the ()* then that means it can not only match this. and this.this. but also this.this.th - in other words, it doesn't necessarily end in a dot, so it will force it to end wherever it has to so that the expression matches. Try it out in a regular expression tester and you'll see what I mean.

    0 讨论(0)
提交回复
热议问题