【问题标题】:how can i take part of string with C# 4.0 Regex我如何使用 C# 4.0 Regex 获取字符串的一部分
【发布时间】:2013-03-08 14:53:37
【问题描述】:
<a href="http://www.try.com"><strong>Try link word</strong></a>  

你好。我怎样才能把这个字符串的一部分

http://www.try.com尝试链接词

使用 c# 4.0 正则表达式?

我试过了,但没用;

var expression = @"<a href=""(.*?)""><strong>(.*?)</strong>";
                        Match m = Regex.Match(text, expression);
                        while (m.Success)
                        {
                            Response.Write("Match: " + m.Groups[0] + " <br> Area code: " + m.Groups[1] +"<br><hr><br>");
                            m = m.NextMatch();
                        }

【问题讨论】:

标签: c# asp.net .net regex c#-4.0


【解决方案1】:

我试过你的代码,得到了这个:

Match: &lt;a href="http://www.try.com"&gt;&lt;strong&gt;Try link word&lt;/strong&gt; &lt;br&gt; Area code: http://www.try.com&lt;br&gt;&lt;hr&gt;&lt;br&gt;

(psst,下次你有一些“不工作”的代码时,把你得到的东西和你期望的一样放在你的问题中)。

变化:

Response.Write("Match: " + m.Groups[0] + " &lt;br&gt; Area code: " + m.Groups[1] +"&lt;br&gt;&lt;hr&gt;&lt;br&gt;");

Response.Write("Match: " + m.Groups[1] + " &lt;br&gt; Area code: " + m.Groups[2] +"&lt;br&gt;&lt;hr&gt;&lt;br&gt;");

给我这个:

Match: http://www.try.com &lt;br&gt; Area code: Try link word&lt;br&gt;&lt;hr&gt;&lt;br&gt;

这就是你想要的吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多