正则表达式,匹配不含“http://”的网址和任何其他“/” [英] Regular Expression, match url without "http://" and any other "/"

查看:187
本文介绍了正则表达式,匹配不含“http://”的网址和任何其他“/”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾了一段时间,但可能我不能用适当的关键词Google,所以我在这里。
我需要匹配一个url剥离协议首先/

I looked around for a while, but probably I can't "Google" with the proper keywords.. so I'm here. I need to match an url stripping out protocol to first /

目标:匹配从http://的第一个子字符串第一个/(可能最后/不存在)或到最后
这里有一个问题:

Target: match the first substring from http:// to first / (maybe last / don't exist) or to the end And here come a problem:

我写了这个正则表达式


(?< = //)(。*?)(?= /)

(?<=//)(.*?)(?=/)

但是这个正则表达式只匹配至少有1个'/'的URL,不包括协议..

but this regex matches only url with at least 1 '/' in the end excluding the protocol..

这里有一些url要匹配:

here some url to be matched:


  • http:// www.google.com /(与我的正则表达式匹配)

  • http:// www.google.com

  • https:// www.google

  • xxx:// www.google.com / hello / bleh blah ...... /

  • xxx:/ /google.com

  • google.com /blah/hello.php?x=11_x.hi

  • http://www.google.com/ (matched by my regex)
  • http://www.google.com
  • https://www.google
  • xxx://www.google.com/hello/bleh blah....../
  • xxx://google.com
  • google.com/blah/hello.php?x=11_x.hi

推荐答案

似乎你有正确的答案,但你e缺少没有拖尾/的可能性。尝试这样:

It seems like you have the right answer, but you're missing the possibility of not having a trailing "/". Try this:

(?<=//)(.*?)(?=/|$)

这篇关于正则表达式,匹配不含“http://”的网址和任何其他“/”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆