将字符串拆分为类

时间:2011-12-13 10:09:05

标签: c# regex string

我有一个包含以下值的数组:

str[0]= "MeterNr 29202"
str[1]="- 20111101: position 61699 (Previous calculation) "
str[2]="- 20111201: position 68590 (Calculation) consumption 6891 kWh"
str[3]="- 20111101: position 75019 (Previous calculation) "
str[4]="MeterNr 50273"
str[5]="- 20111101: position 18103 (Previous reading) "
str[6]="- 20111201: position 19072 (Calculation) consumption 969 kWh "

我想按逻辑顺序拆分行,以便将它们存储在以下的Reading类中。我有分裂值的问题。括号()中的所有内容都是ItemDescription。

我会感谢你的快速回答。

public class Reading
{
    public string MeterNr { get; set; }

    public string ItemDescription { get; set; }

    public string Date { get; set; }

    public string Position { get; set; }

    public string Consumption { get; set; }
}

3 个答案:

答案 0 :(得分:2)

您应该逐个解析这些值。 如果您有一个以“MeterNr”开头的字符串,则应将其保存为currentMeterNumber并进一步解析值。 否则,您可以使用Regex解析值:

var dateRegex = new Regex(@"(?<=-\s)(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})");
var positionRegex = new Regex(@"(?<=position\s+)(\d+)");
var descriptionRegex = new Regex(@"(?<=\()(?<description>[^)]+)(?=\))");
var consuptionRegex = new Regex(@"(?<=consumption\s+)(?<consumption>(?<consumtionValue>\d+)\s(?<consumptionUom>\w+))");

我希望,您将能够创建最终算法,并了解每个表达式的工作原理。最后一点可能是将它们全部组合成单个正则表达式。你应该自己动手来提高你的技能。

P.S。:互联网上有很多教程。

答案 1 :(得分:2)

我只会使用for循环和字符串索引等,但那时我有点简单!不确定你的数据(即如果事情可能会丢失),但这对你发布的数据有用......

var readings = new List<Reading>();
int meterNrLength = "MeterNr".Length;
int positionLength = "position".Length;
int consumptionLength = "consumption".Length;
string meterNr = null;

foreach(var s in str)
{
    int meterNrIndex = s.IndexOf("MeterNr",
                                 StringComparison.OrdinalIgnoreCase);

    if (meterNrIndex != -1)
    {
        meterNr = s.Substring(meterNrIndex + meterNrLength).Trim();
        continue;
    }

    var reading = new Reading {MeterNr = meterNr};

    string rest = s.Substring(0, s.IndexOf(':'));
    reading.Date = rest.Substring(1).Trim();

    rest = s.Substring(s.IndexOf("position") + positionLength);

    int bracketIndex = rest.IndexOf('(');

    reading.Position = rest.Substring(0, bracketIndex).Trim();

    rest = rest.Substring(bracketIndex + 1);

    reading.ItemDescription = rest.Substring(0, rest.IndexOf(")"));

    int consumptionIndex = rest.IndexOf("consumption",
                                        StringComparison.OrdinalIgnoreCase);

    if (consumptionIndex != -1)
    {
        reading.Consumption = rest.Substring(consumptionIndex + consumptionLength).Trim();
    }

    readings.Add(reading);
}

答案 2 :(得分:0)

public static List<Reading> Parser(this string[] str)
{
    List<Reading> result = new List<Reading>();
    string meterNr = "";
    Reading reading;
    foreach (string s in str)
    {
        MatchCollection mc = Regex.Matches(s, "\\d+|\\((.*?)\\)");
        if (mc.Count == 1)
        {
           meterNr = mc[0].Value;
           continue;
        }
        reading = new Reading()
        { 
           MeterNr = meterNr,
           Date = mc[0].Value,
           Position = mc[1].Value,
           ItemDescription = mc[2].Value.TrimStart('(').TrimEnd(')')
        };
        if (mc.Count == 4) 
            reading.Consumption = mc[3].Value;
        result.Add(reading);            
    }
    return result;
}