62,025
社区成员
发帖
与我相关
我的任务
分享
string rl = "300+120";
//如何转换为420,除了Split('+')分割后相加 有其他办法没
StreamReader reader = new StreamReader("c:\\1.txt",Encoding.Default);
while (!reader.EndOfStream)
{
int num = 0;
string str = reader.ReadLine();
Regex reg = new Regex(@"(?is)\d+");
MatchCollection mc = reg.Matches(str);
foreach (Match m in mc)
{
num += Convert.ToInt32(m.Value);
}
MessageBox.Show(num.ToString());
}
string[] strs ={ "5",
"10ml",
"20ML",
"120ml/4",
"360+120ML123afsda23123/3x5",
"360+100ml+sadfasf",
"500ML+300ML+'asdfa123123.21312"};
foreach (string str in strs)
{
MatchCollection ms = new Regex(@"(?<!.+)\d+|(?<=\+)\d+").Matches(str);
int num = 0;
foreach (Match r in ms) { num += int.Parse(r.Value); };
Response.Write(num + "<br/>");
}
string[] strs ={ "5",
"10ml",
"20ML",
"120ml*4",
"360+120ML",
"360+100ml",
"500ML+300ML"};
foreach (string str in strs)
{
var filteredStr=Regex.Replace(str,@"[^\d+*?\d+]", "");
MatchCollection ms = new Regex(@"(?<!.+)\d+|(?<=\+)\d+").Matches(filteredStr);
int num = 0;
foreach (Match r in ms) { num += int.Parse(r.Value); };
Response.Write(num+"<br/>");
string[] source =
{
"5",
"10ml",
"20ML",
"120ml*4",
"360+120ML",
"360+100ml",
"500ML+300ML",
};
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"([^\d\+\*])|(\*\d+)");
System.Data.DataTable dt = new System.Data.DataTable();
foreach (string s in source)
{
string result = dt.Compute(reg.Replace(s, string.Empty), null).ToString();
}
string text = @"5
10ml
20ML
120ml*4
360+120ML
360+100ml
500ML+300ML";
text = Regex.Replace(text, "[a-zA-Z]", "");
text = Regex.Replace(text, @"(?!\d+)(\*.*)", "");
var matches = Regex.Matches(text, @"(\d+)(\+(\d+))*");
foreach (Match match in matches)
{
if (match.Groups[2].Success)
Console.WriteLine(Convert.ToInt32(match.Groups[1].Value)
+
Convert.ToInt32(match.Groups[3].Value));
else
Console.WriteLine(Convert.ToInt32(match.Groups[1].Value));
}