I have some 100 files in which I need to replace:
- Eval(“something”) with Eval(“something”).ToEncodedString()
- Eval(“something”).ToString() with Eval(“something”).ToEncodedString()
I’m writing a small C# application that can automate this. But unable to form the regular expression. Would somebody help me please?
You can use this pattern:
@"\bEval\(""(?<Value>.+?)""\)(?:\.ToString\(\))?"Breakdown:
\bEval: match a word-boundary to ensure we match “Eval” as a whole word and not as part of another word\("": literal opening parenthesis and double quotes (the double quotes appear twice since that’s how they’re escaped when using a verbatim string literal, i.e., the @ symbol preceding the string)(?<Value>.+?): named capture group of “Value” which is a non-greedy match of any character (will stop at double quotes)""\): closing double quotes and closing parenthesis(?:\.ToString\(\))?: the(?:...)bit is a non-capturing group, then we match.ToString()literally with appropriate escaping, and the final?makes this group optionalThe replacement pattern is
@"Eval(""${Value}"").ToEncodedString()", which is easy to understand. The important part is that the named capture group, “Value,” is referenced by using${Value}.Example code: