如何删除最后一个’;’在一个字符串?
如果在字符串末尾发表评论,我需要返回’;’在评论之前。
例:
“第1行//评论2号线;额外文字; //评论也可能……
你没有写下你想对角色做什么,所以我在这里给你一个替代角色的解决方案:
string pattern = "(?<!//.*);(?=[^;]*(//|$))"; Console.WriteLine(Regex.Replace("line 1 //comment", pattern, "#")); Console.WriteLine(Regex.Replace("line2;", pattern, "#")); Console.WriteLine(Regex.Replace("extra; text; //comment may also contain ;.", pattern, "#"));
输出:
line 1 //comment line2# extra; text# //comment may also contain ;.