我想在括号内的文本中插入非字母数字字符。

例如:

"I would like to add * parentheses % around certain text within cells*."


我想通过正则表达式方法将括号内的非字母数字字符放在括号内。

结果:

"I would like to add (*) parentheses (%) around certain text within cells(*)."

最佳答案

string s = Regex.Replace(
    @"I would like to add * parentheses % around certain text within cells*.",
    @"([^.\d\w\s])", "($1)");


或者更具选择性:

string s = Regex.Replace(
    @"I would like to add * parentheses % around certain text within cells*.",
    @"([*%])", "($1)");

09-04 08:45