如果使用 c# 给定的字符串中不存在年,如何拆分年月日?

How to Split Year month Day,if Year does not exist in the given string using c#?

下面的代码用于从给定字符串中拆分数字并将相应的整数存储到 combobox.That 工作 Perfect.But 我想知道,如果 Year 不存在于字符串中,如何将 Year 分配为零并将下一个整数分配给第二个组合框

中的月份 strores

例如:如果字符串是 "4Month(s)2Day(s)" 这里没有年份,那么如何检查年份不包含并将零插入组合框 1,4 到组合框 2 和 2 到组合框 3

在下面的代码中

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;
    }
}

我认为你还有另一个问题。如果您拆分字符串,那么您现在不需要拆分值是年、月还是日。这些信息会随着分裂而丢失。也许您应该以另一种方式解析字符串,以获取此信息。

您可以创建 3 个布尔变量来检查您的字符串中是否有年月日,并在为该组合框赋值之前检查该布尔变量。

if(variable.Contains("Year"))
             bool  Hasyear = true;
if(variable.Contains("Month"))
             bool  HasMonth= true;
if(variable.Contains("Day"))
             bool  HasDay= true;

你可以像这样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

使用更好的模式

           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;​

我不会先拆分字符串然后使用 RegEx 来解析各个部分,而是在整个工作中使用 RegEx。

使用 Regex Hero's tester(需要 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

你可以很简单地这样做:

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;
}