Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将html正文中的hashtag替换为链接,而不匹配C中的内联CSS#_C#_Regex_.net Core - Fatal编程技术网

C# 将html正文中的hashtag替换为链接,而不匹配C中的内联CSS#

C# 将html正文中的hashtag替换为链接,而不匹配C中的内联CSS#,c#,regex,.net-core,C#,Regex,.net Core,因此,我试图替换HTML字符串中的hashtag 我遇到的问题是,它与指向页面中片段的内联css和锚定标记相匹配 以这个html为例: <div style="color:#ffff">#my_tag</div> #我的标签 以及守则: public static string ReplaceHashtag(string input) { Func<string, string> replacer = v => $&quo

因此,我试图替换HTML字符串中的hashtag

我遇到的问题是,它与指向页面中片段的
内联css
锚定标记相匹配

以这个html为例:

<div style="color:#ffff">#my_tag</div>
#我的标签
以及守则:

public static string ReplaceHashtag(string input)
{
    Func<string, string> replacer = v => $"<a href=\"/tags/{v}\">{v}</a>";

    return Regex.Replace(input, @"\b?\#\w*\b", match => replacer(match.Value));
}
公共静态字符串替换hashtag(字符串输入)
{
Func replacer=v=>$“”;
返回Regex.Replace(输入@“\b?\\w*\b”,match=>Replace(match.Value));
}

将用锚标记替换css属性和hashtag。

我还没有用大量输入测试过这一点(只是OP中提供的示例输入),但这种模式似乎适合我:

        var pattern = @">.*(#\w*).*<";
        var tempHtml = "<div style=color:#ffff>#my_tag</div>";
        var ms = Regex.Match(tempHtml, pattern, RegexOptions.IgnoreCase);
        var tag = ms.Groups.Count > 0 ? ms.Groups[1].Value : string.Empty;

var pattern=@“>.(#\w*).......#(\w*).*也许您可以提供一些示例数据?很好,完成了。
var pattern = @">.*#(\w*).*<";