Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to validate URIs

Tags:

How does one go about authoring a Regular Expression that matches against all strings that are valid URIs, while failing to match against all strings that are invalid URIs?

To be specific about what I am referencing when I say URI, I have added a link below for the most current URI RFC standard. It defines the entity that I want to validate using a regular expression.

I don't need it to be able to parse the URI. I just need a regular expression for validating.

The .Net Regular Expression Format is preferred. (.Net V1.1)


My Current Solution:

^([a-zA-Z0-9+.-]+):(//([a-zA-Z0-9-._~!$&'()*+,;=:]*)@)?([a-zA-Z0-9-._~!$&'()*+,;=]+)(:(\\d*))?(/?[a-zA-Z0-9-._~!$&'()*+,;=:/]+)?(\\?[a-zA-Z0-9-._~!$&'()*+,;=:/?@]+)?(#[a-zA-Z0-9-._~!$&'()*+,;=:/?@]+)?$(:(\\d*))?(/?[a-zA-Z0-9-._~!$&'()*+,;=:/]+)?(\?[a-zA-Z0-9-._~!$&'()*+,;=:/?@]+)?(\#[a-zA-Z0-9-._~!$&'()*+,;=:/?@]+)?$
like image 695
alumb Avatar asked Aug 27 '08 18:08

alumb


1 Answers

Does Uri.IsWellFormedUriString work for you?

like image 194
bdukes Avatar answered Sep 19 '22 19:09

bdukes