C#字符串中正则表达式匹配指定字符串段删除其它字符串
C#字符串中正则表达式匹配指定字符串段删除其它字符串如下字符1P+NC630mA(710063300000002)1P+NC630mA(710063300000002)1...
C#字符串中正则表达式匹配指定字符串段删除其它字符串
如下字符
1P+N C6 30mA (710063300000002)
1P+N C6 30mA (710063300000002)
1P+N C10 30mA (710063300000003)
1P+N C16 30mA (710063300000004)
1P+N C20 30mA (710063300000005)
1P+N C25 30mA (710063300000006)
1P+N C32 30mA (710063300000007)
1P+N C40 30mA (710063300000008)
2P C6 30mA (710063300000009)
2P C10 30mA (710063300000010)
2P C16 30mA (710063300000011)
2P C20 30mA (710063300000012)
2P C25 30mA (710063300000013)
2P C32 30mA (710063300000014)
2P C40 30mA (710063300000015)
3P C6 30mA (710063300000016)
3P C10 30mA (710063300000017)
3P C16 30mA (710063300000018)
3P C20 30mA (710063300000019)
3P C25 30mA (710063300000020)
3P C32 30mA (710063300000021)
上图如需要两种操作方式:
删除括号内的所有字符串?
删除除括号外的所有字符串? 展开
如下字符
1P+N C6 30mA (710063300000002)
1P+N C6 30mA (710063300000002)
1P+N C10 30mA (710063300000003)
1P+N C16 30mA (710063300000004)
1P+N C20 30mA (710063300000005)
1P+N C25 30mA (710063300000006)
1P+N C32 30mA (710063300000007)
1P+N C40 30mA (710063300000008)
2P C6 30mA (710063300000009)
2P C10 30mA (710063300000010)
2P C16 30mA (710063300000011)
2P C20 30mA (710063300000012)
2P C25 30mA (710063300000013)
2P C32 30mA (710063300000014)
2P C40 30mA (710063300000015)
3P C6 30mA (710063300000016)
3P C10 30mA (710063300000017)
3P C16 30mA (710063300000018)
3P C20 30mA (710063300000019)
3P C25 30mA (710063300000020)
3P C32 30mA (710063300000021)
上图如需要两种操作方式:
删除括号内的所有字符串?
删除除括号外的所有字符串? 展开
3个回答
2015-11-16
展开全部
string str = "1P+N C6 30mA (710063300000002)\r\n 1P+N C6 30mA (710063300000002)\r\n 1P+N C10 30mA (710063300000003)\r\n 1P+N C16 30mA (710063300000004)\r\n 1P+N C20 30mA (710063300000005)\r\n 1P+N C25 30mA (710063300000006)\r\n 1P+N C32 30mA (710063300000007)\r\n 1P+N C40 30mA (710063300000008)\r\n 2P C6 30mA (710063300000009)\r\n 2P C10 30mA (710063300000010)\r\n 2P C16 30mA (710063300000011)\r\n 2P C20 30mA (710063300000012)\r\n 2P C25 30mA (710063300000013)\r\n 2P C32 30mA (710063300000014)\r\n 2P C40 30mA (710063300000015)\r\n 3P C6 30mA (710063300000016)\r\n 3P C10 30mA (710063300000017)\r\n 3P C16 30mA (710063300000018)\r\n 3P C20 30mA (710063300000019)\r\n 3P C25 30mA (710063300000020)\r\n 3P C32 30mA (710063300000021)";
//删除括号内的所有字符串
Regex reg = new Regex("\\([^\\(\\)]+\\)");
string str2 = reg.Replace(str, "()");
Console.WriteLine(str2);
Console.WriteLine("===============================");
//删除除括号外的所有字符串
Regex reg1 = new Regex("[^\\(\\)\r\n]+(\\([^\\(\\)]+\\))");
string str3 = reg1.Replace(str, "$1");
Console.WriteLine(str3);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询