开发者

Regex.Replace only replaces start of string

I'm trying to replace a friendly url pattern with a html url notation but due to lack of regex experience I can't figure out why my regex only replaces the first occurence of my pattern:

string text = "[Hotel Des Terrasses \http://flash-hotel.fr/] and [Du Phare \http://www.activehotels.com/ho开发者_StackOverflow中文版tel/]";
text = Regex.Replace(text, @"\[(.+)\s*\\(.+)\]", "<a href=\"$2\" target=\"_blank\">$1</a>");

How can i make the second pattern be replaced with the HTML markup too?


Your regex treats the entire string as a single match. Try using (.+?) instead of (.+) (both instances).


As an aside note, you might want to consider potential abuse of this. You should probably perform:

        StringBuilder sb = new StringBuilder();
        int pos = 0;

        Regex exp = new Regex(@"\[(.+?)\s*\\(.+?)\]");
        foreach (Match m in exp.Matches(text))
        {
            sb.Append(text, pos, m.Index - pos);
            pos = m.Index + m.Length;

            Uri tmp;
            if(Uri .TryCreate(m.Groups[2], UriKind.Absolute, out tmp))
            {
                sb.AppendFormat("<a href=\"{0}\" target=\"_blank\">{1}</a>",
                    System.Web.HttpUtility.HtmlAttributeEncode(tmp.AbsoluteUri),
                    System.Web.HttpUtility.HtmlEncode(m.Groups[1])
                    );
            }
        }
        sb.Append(text, pos, text.Length - pos);

Note: Not sure of the group indexes, I'd use named groups in the reg-ex. Have you tried a regex tool like Expresso?


The regular expression takes the longest match, which in this case is the entire string, because your conditions are that it starts with a [, ends with a ] and has at least one backslash somewhere in between. Re-specify the regular expression so as not to allow another ] inside the brackets, e.g. use [^\]] instead of . (both occurrences).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜