我想从字符串中删除[数字];
输入示例是:
1 - 家庭住址 [我的第一个家庭住址 #12] [1350]
2 - 这里有一些文字(这里有一些文字)一些
3 - hello dolor sat amet [kost street #74] [1008]
输出示例是:
1 - 家庭住址 [我的第一个家庭住址 #12]
2 - 这里有一些文字(这里有一些文字)一些
3 - hello dolor sat amet [kost street #74]
我尝试过的
var pattern = @"^[\d+]$";
var result = Regex.Replace(value, pattern, "");
我想删除带有二次符号“[”和“]”的粗体文本,如果它根本不包含它,则保持原样(请参阅第二个示例)。
最佳答案
您尝试的模式"^[\d+]$
几乎是正确的!该模式的问题是您使用的 anchor ^...$
基本上表示该模式仅匹配大括号中封装的数字(如果它们是输入中唯一的字符串)。See the MSDN documentation on Anchors for a more technical summary.
模式 \[\d+\]
将匹配方括号中封装的任何数字。
使用Regex.Replace
:-
input = Regex.Replace(input, @"\[\d+\]", string.Empty);
关于c# - 从字符串中替换 [number] 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17380604/