用于在C#中验证URL的正则表达式 [英] Regex for validate url in C#

查看:81
本文介绍了用于在C#中验证URL的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网址格式是

 href =https://a.abc.io/00c8 





我尝试过:



 ^(http | https):\ / \ / www。[ A-Z0-9] +(\(COM |组织| EDU |网络|信息|商业|政府网站|协同|中| IO)\ /?)([A-ZA-Z0-9] +)? $ 





但它不起作用

解决方案





但它不起作用


它不起作用,因为您显示的URL示例不以www开头:

 href =https://a.abc.io/00c8

老实说,使用正则表达式解析HTML是众所周知的糟糕的主意 - 使用像 Html Agility Pack |这样的东西会更好。 HAP [ ^ ],这使得这样的网站整个负载更容易。



但是......如果你要使用正则表达式,那么得到 Expresso [ ^ ] - 这是free,它会检查并生成正则表达式。


引用:

但它不起作用



简单,您的网址是以'.io'结尾的网站,但您的RegEx仅接受以'com | org | edu | net | info | biz | gov | co |结尾的网站'。

您需要在列表中添加'io'。

由于OG说www是另一个问题,因为您的网址不使用它。



只需几个有趣的链接来帮助构建和调试RegEx。

以下是RegEx文档的链接:

<一个人f =http://perldoc.perl.org/perlre.html> perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:

.NET Regex Tester - Regex Storm [ ^ ]

Expresso正则表达式工具 [ ^ ]

RegExr:Learn,Build,&测试RegEx [ ^ ]

在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]

这个网站也在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容:

Regexper [ ^ ]


my url format is

href="https://a.abc.io/00c8



What I have tried:

^(http|https):\/\/www.[a-z0-9]+(\.(com|org|edu|net|info|biz|gov|co|in|io)\/?)([a-zA-Z0-9]+)?$



but its not working

解决方案



but its not working


It's not working because the URL example you show doesn't start with "www":

href="https://a.abc.io/00c8

To be honest, parsing HTML with a regex is well known as a bad idea - you would be much, much better off using something like the Html Agility Pack | HAP[^] which makes such site scraping a whole load easier.

But ... if you are going to use regexes, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.


Quote:

but its not working


Simple, your url is a site that end with '.io' but your RegEx only accept sites that end with 'com|org|edu|net|info|biz|gov|co|in'.
You need to add 'io' to the list.
As OG said 'www' is another problem because your url don't use it.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]


这篇关于用于在C#中验证URL的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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