Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for valid URL characters

Tags:

c#

regex

I am trying to check a string before saving it to my database,

here is an example string "Paint & Brush"

now the & is invalid, how can I use a Regex to detect this, other characters I want to check for these charaters £, $, %, # etc

I have tried this

Regex RgxUrl = new Regex(@"[^A-Z0-9.\-\)\(]");

however the "paint & brush" example from before was still valid

like image 502
Canvas Avatar asked Dec 25 '22 12:12

Canvas


2 Answers

Validating URLs is a common problem, so you should first consider using the available tools to do it instead of reinventing the wheel. Nevertheless, from wikipedia:

Unreserved

May be encoded but it is not necessary

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~

Reserved

Have to be encoded sometimes

! * ' ( ) ; : @ & = + $ , / ? % # [ ]

Further details can for example be found in RFC 3986 and http://www.w3.org/Addressing/URL/uri-spec.html.

Based on this, your pattern would be [^-\]_.~!*'();:@&=+$,/?%#[A-z0-9]. You want to see if this (exclusive) patten matches any characters in your string, if so, those are probably special characters that must be encoded.

Code generated by RegexBuddy:

bool hasInvalidChars = false;
try {
    hasInvalidChars = Regex.IsMatch(urlToTest, @"[^-\]_.~!*'();:@&=+$,/?%#[A-z0-9]", RegexOptions.Singleline | RegexOptions.Multiline);
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}
like image 87
Superbest Avatar answered Jan 05 '23 17:01

Superbest


Why not

Uri.IsWellFormedUriString(stringURL, UriKind.RelativeOrAbsolute)

Read more Uri.IsWellFormedUriString Method

Or Uri.TryCreate Method

like image 32
huMpty duMpty Avatar answered Jan 05 '23 15:01

huMpty duMpty