C# 将字符串拆分为类

C# 将字符串拆分为类,c#,regex,string,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 502

我有一个数组,其中包含以下值:

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 "
我想按逻辑顺序拆分这些行,以便在下面的阅读课中存储它们。我在拆分值时遇到问题。括号()中的所有内容都是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; }
}

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

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+))");

var dateRegex=newregex(@)(?您应该逐个解析这些值。
如果您有一个以“meterner”开头的字符串,则应将其另存为
currentMeterNumber
,并进一步解析这些值。 否则,可以使用正则表达式解析值:

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+))");

var dateRegex=new Regex(@“(?我只会使用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);
}
var读数=新列表();
int meternlength=“meterner.”长度;
int positionLength=“position”。长度;
int consumptionLength=“consumption.”长度;
字符串meterNr=null;
foreach(str中的var s)
{
int meterNrIndex=s.IndexOf(“MeterNr”,
对照组(例);
如果(MeternIndex!=-1)
{
meterNr=s.Substring(meterNrIndex+meterNrLength).Trim();
继续;
}
var reading=新读数{MeterNr=MeterNr};
字符串rest=s.Substring(0,s.IndexOf(':');
reading.Date=rest.Substring(1.Trim();
rest=s子串(s.IndexOf(“位置”)+位置长度);
int bracketIndex=rest.IndexOf('(');
reading.Position=rest.Substring(0,bracketIndex).Trim();
rest=rest.子字符串(括号索引+1);
reading.itemsdescription=rest.Substring(0,rest.IndexOf(“)”);
int consumptionIndex=rest.IndexOf(“消费”,
对照组(例);
if(消耗指数!=-1)
{
reading.consumpion=rest.Substring(consumptionIndex+consumptionLength).Trim();
}
增加(阅读);
}

我只想使用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);
}
var读数=新列表();
int meternlength=“meterner.”长度;
int positionLength=“position”。长度;
int consumptionLength=“consumption.”长度;
字符串meterNr=null;
foreach(str中的var s)
{
int meterNrIndex=s.IndexOf(“MeterNr”,
对照组(例);
如果(MeternIndex!=-1)
{
meterNr=s.Substring(meterNrIndex+meterNrLength).Trim();
继续;
}
var reading=新读数{MeterNr=MeterNr};
字符串rest=s.Substring(0,s.IndexOf(':');
reading.Date=rest.Substring(1.Trim();
rest=s子串(s.IndexOf(“位置”)+位置长度);
int bracketIndex=rest.IndexOf('(');
reading.Position=rest.Substring(0,bracketIndex).Trim();
rest=rest.子字符串(括号索引+1);
reading.itemsdescription=rest.Substring(0,rest.IndexOf(“)”);
int consumptionIndex=rest.IndexOf(“消费”,
对照组(例);
if(消耗指数!=-1)
{
reading.consumpion=rest.Substring(consumptionIndex+consumptionLength).Trim();
}
增加(阅读);
}
公共静态列表解析器(此字符串[]str)
{
列表结果=新列表();
字符串meterner=“”;
阅读;
foreach(str中的字符串s)
{
MatchCollection mc=Regex.Matches(s,“\\d+\\(.*?\)”);
如果(mc.Count==1)
{
meterNr=mc[0]。数值;
继续;
}
阅读=新阅读()
{ 
计量器=计量器,
日期=mc[0]。值,
位置=mc[1]。值,
ItemDescription=mc[2]。Value.TrimStart(“(”).trimsend(“)”)
};
如果(mc.Count==4)
reading.consumpion=mc[3]。值;
结果。添加(读取);
}
返回结果;
}
公共静态列表解析器(此字符串[]str)
{
列表结果=新列表();
字符串meterner=“”;
阅读;
foreach(str中的字符串s)
{
MatchCollection mc=Regex.Matches(s,“\\d+\\(.*?\)”);
如果(mc.Count==1)
{
meterNr=mc[0]。数值;
继续;
}
阅读=新阅读()
{ 
计量器=计量器,
日期=mc[0]。值,
位置=mc[1]。值,
ItemDescription=mc[2]。Value.TrimStart(“(”).trimsend(“)”)
};
如果(mc.Count==4)
reading.consumpion=mc[3]。值;
结果。添加(读取);
}
返回结果;
}

请您更具体一点好吗?输出应该是什么样子的?换句话说,应该返回什么MeterNr、ItemDescription等。有几个选项。一个是检查(IndexOf)开始(和结束)并获取里面的内容。或者您可以使用正则表达式来完成。尝试用英语描述(或您选择的语言)确切地说,您想从每一行中得到什么,这应该给您一个关于您应该做什么的提示。进一步的提示:您似乎不需要正则表达式,一个简单的IndexOf/子字符串已经可以帮助您了。@all-over:str[2]的示例输出:MeterNr=29202,Description=Calculation,Date=20111201,Position=68590,消耗量=6891千瓦,str[4]MeterNr变为50273后,对吗?那么str[5]和str[6]中是否也使用了该参数?您能更具体一点吗?输出应该是什么样子的?换句话说,应该返回什么MeterNr、itemsdescription等。有几个选项。一个是检查(IndexOf)开始(和结束)