简体   繁体   中英

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/hotel/]";
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, eg use [^\\]] instead of . (both occurrences).

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