Canvas Canvas - 1 year ago 223
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 Source

Why not

Uri.IsWellFormedUriString(stringURL, UriKind.RelativeOrAbsolute)

Read more Uri.IsWellFormedUriString Method

Or Uri.TryCreate Method

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download