开发者

.Net regular expression replace - prepend each word with certain character

开发者 https://www.devze.com 2022-12-25 15:13 出处:网络
Input \"col1, col2, col3, coln\"开发者_运维知识库 Output \"@col1, @col2, @col3, @coln\"Try this:

Input "col1, col2, col3, coln"

开发者_运维知识库

Output "@col1, @col2, @col3, @coln"


Try this:

string input = "col1, col2, col3, coln";
string pattern = @"\b(\w+)\b";
string result = Regex.Replace(input, pattern, "@$1");
Console.WriteLine(result);


string input = "col1, col2, col3, coln";
string result = "@" + Regex.Replace(input, @"\s+", " @");

It depends how robust you want it to be, of course. But if it's just for SQL query parameters, which is what it appears to be, then the above should be fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消