简体   繁体   中英

URL display with proper output using System.Uri c#

I have an application where in I have stored a lot of websites without validating them. Now I am validating the URL entered. But the already stored URL's are there as it is.

I want a strict display code that allows me to correct the user typos also and just gives the a proper URL to deal with.

The data that is already in the system has a lot of typos such as ... http://example.com or htp://example.com or ttp://example.com. I want the code to tackle that and come up with the proper url either by regexing the invalid part or making it correct.

That is the best approach to establish this?

You can obviously pick out the correct ones with a regex.

However, you will need to write your own logic to fix those that are 'broken'. You could pull these and with another regex and then simply search and replace the broken element. There are going to be limitations to this as you can only really check the protocol prefix and not the domain part itself.

Here is my try:

http(s)?://(www.)?[a-zA-Z0-9\-\.\\/]+

where [a-zA-Z0-9-.\\/] includes all characters that you want to allow users to use.

PS please be aware that if you are using RegEx under C#, do not forget to use double \\\\ as otherwise your expression might not work properly.

Hope it gets you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM