簡體   English   中英

如何從字符串中解析鏈接

[英]How to parse links from the string

我有一個字符串,例如“轉到堆棧溢出http://stackoverflow.com ”現在我想使用 c# 解析該字符串中的鏈接。 我想要以下格式的 output。 " Go to stack overflow <a href=http://stackoverflow.com target=_blank>http://stackoverflow.com</> "

C# 有可能嗎?

使用具有此模式的正則表達式^(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</>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM