tags:

views:

140

answers:

1

My regex after a String.Format(c#):

(^{0})|({0}[-!#$%&'}()+,./:;<=>?@\{{|~"])|([-!#$%&'()*+,./}:;<=>?@_{|~"]{0})|( {0} )

When the regex is coming through the debugger it looks like this:

(^ Word )|( Word [-!#$%&'}()+,./:;<=>?@\{|~"])|([-!#$%&'()*+,./}:;<=>?@_{|~"] Word )|( Word )

The curly braces for my string.format have been replaced with spaces, which obviously breaks my regex. Does anyone know how to solve this problem??

+4  A: 

String.Format does not add spaces when it substitutes the value in. My guess is your word is the culprit. Where does it come from? Have you tried trimming it before the format call?

public Regex getExpression(string word)
{
    string baseExpr = @"(^{0})|({0}[-!#$%&'}()+,./:;<=>?@\{{|~""])|([-!#$%&'()*+,./}:;<=>?@_{|~""]{0})|( {0} )";

    return new Regex(string.Format(baseExpr, word.Trim()));
}

You may want to sanitize the word even more to strip out key regex characters.

Joel Coehoorn
+1 I think that an untrimmed string is the culprit.
Andrew Hare
Your code sample crashes with a FormatException in the string.Format() call.
Iceman
I just copied his expression verbatim and added " characters to the syntax highlighting works. Looks like he didn't escape all the { characters correctly.
Joel Coehoorn
to "sanitize" the word I'd suggest to use at least Regex.Escape() because otherwise you have a "regex injection" possibility ;)
Lucero