ASP.NET RegEx: How do I replace all occurances of Eval(?????)
I have this which occurs hundreds of times in our project (with one space on both sides):
Eval("RandomDataName")
I wish to replace this with (with one space on both sides):
H(Eval("RandomDataName"))
Right now I have,
Eval\({.*}\)
And replace with:
H(Eval(\1))
This works, but not on lines in which there are multiple Eval, as it selects all of them on the same line. How can I fix this? I've tried using .*?开发者_如何学运维 and that doesn't work.
You need to be more explicit, the current regex is too greedy.
For instance, say you have this on a line.
Eval("Test") Eval("Another") Eval("Yet one more")
Your regex will match Eval(
and proceed to the very last )
, capturing everything in between. The easiest way to get this to work would likely be to have a "whitelist" of characters you expect. Something like:
Eval\([a-zA-Z0-9\" ]*\)
This would match Eval(
, then match quotes, alphanumeric characters, and spaces zero or more times and then finally match the closing )
of each Eval()
statement.
I would recommend tossing some sample Eval()
statements into RegExr and testing out some regexes and see what works for your data set.
精彩评论