[英]How to Split Year month Day,if Year does not exist in the given string using c#?
在这里下面的代码正在为从给定的字符串中拆分数字并将相应的整数存储到combobox中。该工作完美。但是我想知道,如果字符串中不存在Year,如何将Year分配为零,然后将下一个赋值第二个组合框中表示月份的整数
例如:如果字符串为“ 4Month(s)2Day(s)”,这里没有Year,那么如何检查Year不包含,并在combobox1,4到combobox2中插入零,在combobox2中插入2,
在以下代码中
int count = 0;
string[] delimiterChars = {"Year","Years","Years(s)","Month","Month(s)","Day","Day(s)"};
string variable =agee;
string[] words = variable.Split(delimiterChars, StringSplitOptions.None);
foreach (string s in words)
{
var data = Regex.Match(s, @"\d+").Value;
count++;
if (count == 1)
{
comboBox1.Text = data;
}
else if (count == 2)
{
comboBox2.Text = data;
}
else if (count == 3)
{
comboBox3.Text = data;
}
}
您可以像这样使用Regex
int combBox1, combBox2, combBox3;
var sample = "1Year(s)4month(s)2DaY(s)";
var yearString = Regex.Match(sample, @"\d+Year", RegexOptions.IgnoreCase).Value;
if (!string.IsNullOrEmpty(yearString))
combBox1 = int.Parse(Regex.Match(yearString, @"\d+").Value);
var monthString = Regex.Match(sample, @"\d+Month", RegexOptions.IgnoreCase).Value;
if (!string.IsNullOrEmpty(monthString))
combBox2 = int.Parse(Regex.Match(monthString, @"\d+").Value);
var dayStrings = Regex.Match(sample, @"\d+Day", RegexOptions.IgnoreCase).Value;
if (!string.IsNullOrEmpty(dayStrings))
combBox3 = int.Parse(Regex.Match(dayStrings, @"\d+").Value);
您可以根据需要跳过int.Parse()
,然后必须手动设置0
。
我将首先使用RegEx来完成整个工作,而不是先拆分字符串然后再使用RegEx来解析部分。
使用Regex Hero的测试器 (需要Silverlight才能运行...),我想到了以下内容:
(?:(?<years>\d+)Year\(?s?\)?)?(?<months>\d+)Month\(?s?\)?(?<days>\d+)Day\(?s?\)?
这与以下所有输入匹配
Input Matching groups:
***** ****************
4Month(s)2Day(s) months: 4, days: 2
1Year(s)4Month(s)2Day(s) years: 1, months: 4, days: 2
3Years6Month(s)14Day(s) years: 3, months: 6, days: 14
1Year1Month1Day years: 1, months, 1, days: 1
如您所见,它匹配那里的所有内容。 如果多年没有匹配项,则可以使用捕获组的“ Success
属性进行测试。
样品
var pattern = @"(?:(?<years>\d+)Year\(?s?\)?)?(?<months>\d+)Month\(?s?\)?(?<days>\d+)Day\(?s?\)?";
var regex = new Regex(pattern);
var testCases = new List<string> {
"4Month(s)2Day(s)",
"1Year(s)4Month(s)2Day(s)",
"3Years6Month(s)14Day(s)",
"1Year1Month1Day"
};
foreach (var test in testCases) {
var match = regex.Match(test);
var years = match.Groups["years"].Success ? match.Groups["years"].Value : "0";
var months = match.Groups["months"].Value;
var days = match.Groups["days"].Value;
string.Format("input: {3}, years: {0}, months: {1}, days: {2}", years, months, days, test).Dump();
}
在LinqPad中运行它,您将看到
input: 4Month(s)2Day(s), years: 0, months: 4, days: 2
input: 1Year(s)4Month(s)2Day(s), years: 1, months: 4, days: 2
input: 3Years6Month(s)14Day(s), years: 3, months: 6, days: 14
input: 1Year1Month1Day, years: 1, months: 1, days: 1
我认为您在这里还有另一个问题。 如果拆分字符串,则现在不包含值是年,月或日。 该信息会因拆分而丢失。 也许您应该以另一种方式解析字符串,以获取此信息。
您可以创建3个布尔变量,以检查字符串中是否有年份和月份,并在将该值分配给该组合框之前检查该布尔变量。
if(variable.Contains("Year"))
bool Hasyear = true;
if(variable.Contains("Month"))
bool HasMonth= true;
if(variable.Contains("Day"))
bool HasDay= true;
使用更好的模式
string input1 = "1Year(s)4Month(s)2Day(s)"; string pattern1 = @"(?'year'\\d+)?(Year(\\(s\\))?)?(?'month'\\d+)(Month(\\(s\\))?)?(?'day'\\d+)(Day(\\(s\\))?)?"; Match match1 = Regex.Match(input1, pattern1); string year1 = match1.Groups["year"].Value; string month1 = match1.Groups["month"].Value; string day1 = match1.Groups["day"].Value; string input2 = "4Month(s)2Day(s)"; string pattern2 = @"(?'year'\\d+)?(Year(\\(s\\))?)?(?'month'\\d+)(Month(\\(s\\))?)?(?'day'\\d+)(Day(\\(s\\))?)?"; Match match2 = Regex.Match(input2, pattern2); string year2 = match2.Groups["year"].Value; string month2 = match2.Groups["month"].Value; string day2 = match2.Groups["day"].Value;
您可以非常简单地这样做:
string agee = "1Year4Month(s)2Day(s)";
string[] delimiterChars = {"Year", "Month", "Day"};
string variable =agee.Replace("(s)","").Replace("s", "");
string[] words = variable.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
int count = words.Length;
switch (count)
{
case 0:
combobox1.Text = "0";
combobox2.Text = "0";
combobox3.Text = "0";
break;
case 1:
combobox1.Text = "0";
combobox2.Text = "0";
combobox3.Text = words[0];
break;
case 2:
combobox1.Text = "0";
combobox2.Text = words[0];
combobox3.Text = words[1];
break;
case 2:
combobox1.Text = words[0];
combobox2.Text = words[1];
combobox3.Text = words[2];
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.