hcbin

来源:https://segmentfault.com/q/1010000000584340/a-1020000000584362

 

Markdown 的作者之一写的正则表达式(原文在这

(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))

测试代码:

// 注意,这里把上面的正则表达式中的单引号用反斜杠转义了,不然没法放在字符串里
$regex = \'@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\\'".,<>?«»“”‘’]))@\';

var_dump( preg_match($regex, \'http://segmentfault.com/q/1010000000584340\') );  //true

 

In search of the perfect URL validation regex

https://mathiasbynens.be/demo/url-regex

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-11-03
  • 2022-12-23
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2021-12-13
相关资源
相似解决方案