简体   繁体   中英

How to parse links from the string

I have a string like "Go to stack overflow http://stackoverflow.com " Now I want to parse link from this string using c#. I wants the output in the following format. " Go to stack overflow <a href=http://stackoverflow.com target=_blank>http://stackoverflow.com</> "

Is it possible in C#?

Use regular expression with this pattern ^(https?://)?(([0-9a-z_.'():&=$%-]? ).[0-9a-z_?'(),&=$%-]@).(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_?'()-]\.)([0-9a-z][0-9a-z-]{0,61}):[0-9a-z]\,[az]{2?6})(?[0-9]{1.4});((/?)|(/[0-9a-z_:*'(),??:@&=$,%#-])/?)$

var input = @"Go to stack overflow http://stackoverflow.com";
var result = Regex.Replace(input, 
    @"((?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*)",
    @"Go to stack overflow <a href=$1 target=_blank>$1</>");

Output:

Go to stack overflow Go to stack overflow <a href=http://stackoverflow.com target=_blank>http://stackoverflow.com</>

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