正则表达式匹配捕获域名 [英] Regex to match a capture a domain name
本文介绍了正则表达式匹配捕获域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将获得以下格式的网址:
Hi i will have URL in following format:
- http://www.youtube.com/v/0PsnoiwMrhA
- https://www.youtube.com/v/0PsnoiwMrhA
- www.youtube.com/v/0PsnoiwMrhA
- http://youtube.com/v/0PsnoiwMrhA
- youtube.com/v/0PsnoiwMrhA
这一切都必须以 youtube
的形式捕获并返回域名.
It all must capture and return a domain name as youtube
.
我试过使用
(http://|https://)?(www.)(.?*)(.com|.org|.info|.org|.net|.mobi)
但它显示错误为regex parsing nested quantifier
.
请帮帮我
推荐答案
如果您使用的字段是这些格式之一,您可以使用以下正则表达式从 Group 1 中检索匹配项:
If you are using a field that you know is in one of these formats, you can retrieve the match from Group 1 using this regex:
^(?:https?://)?(?:www\.)?([^.]+)
在 VB.NET 中:
In VB.NET:
Dim ResultString As String
Try
ResultString = Regex.Match(SubjectString, "^(?:https?://)?(?:www\.)?([^.]+)", RegexOptions.Multiline).Groups(1).Value
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try
这篇关于正则表达式匹配捕获域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文