tags:

views:

150

answers:

3

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?

+2  A: 

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?

csharptest.net
This isn't the problem. (`Regex.Replace` replaces _all_ matches)
SLaks
Wasn't sure as I normally don't use the static version of the replace, I'll update.
csharptest.net
thanks for the suggestion, great idea. But in my scenario not needed as it's not web input.
Yannick Smits
+4  A: 

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

Marcelo Cantos
This is the correct answer. (You need to change both `.+`'s)
SLaks
Thanks for the qualifier, @SLaks.
Marcelo Cantos
awesome thanks!
Yannick Smits
+1  A: 

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).

Arkku
This is also the correct answer.
SLaks