0

我问一个关于 anology 上下文的问题,假设我有不同形式的日期,我想将它们转换为一种格式。我通过使用下面的代码做到了这一点

string[] dates =
                          {
                "december-21-2016",
                "december, 28,2016",
                "27 january,2017",
                "29 december,2016",
                "03 mar 1990"

                            };
        string[] formattedDates = new string[dates.Length];

        string[] formats = { "MMMM-dd-yyyy","MMMM, dd,yyyy","MMMM, d,yyyy","dd MMMM,yyyy","dd MMM yyyy" };
        for (int i = 0; i < dates.Length; i++)
        {
            DateTime date;
            if (DateTime.TryParseExact(dates[i], formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                formattedDates[i] = date.ToString("dd/MM/yyyy");
            Console.WriteLine(formattedDates[i]);
        }

这是输出 21-12-2016, 28-12-2016, 27-01-2017, 29-12-2016, 03-03-1990,

我想编写相同的代码,但要使用文本而不是日期

string[] formats=
           {
            "Husband and Wife",
            "Wife and Husband",
            "Husband and Wife",
            "Wife and Husband",
            "hus & wife",
            "H & W",               
            };

但是对于上面的所有格式,输出应该是“H and W”。如果我们可以使用 linq 做到这一点,那就更棒了。

抱歉英语和格式不好,我是新手。

4

1 回答 1

0

虽然DateTime有一种基于格式解析它们的简单方法,但其他文本形式可能更难处理。

我不确定 Regex 是否最适合这种情况,但是,您可以从下面的代码中激发自己的灵感,以实现您的文本解析:

public class RegexTextCalibrator
{
    private ICollection<Tuple<Regex, string>> Calibrations = new List<Tuple<Regex, string>>();

    public void AddParallelPattern(IEnumerable<string> a, IEnumerable<string> b, string output)
    {
        this.AddParallelPattern(a, b, @"\W+.*", output);
    }

    public void AddParallelPattern(IEnumerable<string> a, IEnumerable<string> b, string inBetweenLinkPattern, string output)
    {
        string subPatternA = String.Join("|", a);
        string subPatternB = String.Join("|", b);

        var patternA = String.Format("({0})({1})({2})", subPatternA, inBetweenLinkPattern, subPatternB);
        var patternB = String.Format("({0})({1})({2})", subPatternB, inBetweenLinkPattern, subPatternA);

        this.Add(String.Format("{0}|{1}", patternA, patternB), output);
    }

    public void Add(string pattern, string output)
    {
        this.Add(new Regex(pattern, RegexOptions.IgnoreCase), output);
    }

    public void Add(Regex regex, string output)
    {
        Calibrations.Add(new Tuple<Regex, string>(regex, output));
    }

    public string Resolve(string value)
    {
        var calibration = this.Calibrations.FirstOrDefault(x => x.Item1.IsMatch(value));
        return calibration != null ? calibration.Item2 : null;
    }
}

编辑下面是上述代码的注释用法。

您可以按如下方式使用该类:

// configure the calibration/mapping.
var calibrator = new RegexTextCalibrator();
calibrator.AddParallelPattern(
                new[] { "husband", "hus", "h" }, // defines one side of the pattern
                new[] { "wife", "w" }, // defines the other side of the pattern
                "H and W"); // defines the result

// the inputs that will be mapped
var inputs = new string[] {
    "Husband and Wife",
    "Wife and Husband",
    "Husband and Wife",
    "Wife and Husband",
    "hus & wife",
    "H & W"
};

// loops through the inputs and map them to the desired value
foreach (var input in inputs)
    Console.WriteLine(calibrator.Resolve(input)); // prints "H and W"...
于 2017-03-14T17:05:51.283 回答