Canvas Canvas - 4 months ago 40
C# Question

Regex for valid URL characters

Hey guys i'm 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

Answer

Why not

Uri.IsWellFormedUriString(stringURL, UriKind.RelativeOrAbsolute)

Read more Uri.IsWellFormedUriString Method

Or Uri.TryCreate Method

Comments