将'00'和'0'替换为inf文件部分中的'+'将新行添加到新INF

时间:2013-04-23 09:44:42

标签: c#

我正在学习C#并且我有一些.inf文件(参见下面的示例)我需要能够更改[Destinations]部分,其中一行以“00”或“0”开头,带有“+” 。我尝试过使用Regex.Replace,但它会替换文件中存在“00”的所有条目。什么是好的,但在这一点上没有essintial将保留文件中的两个条目。 IE

[Destinations]
0="National",N,N
+="National",N,N
0024195="Gabon Mobiles",I,IMT
+24195="Gabon Mobiles",I,IMT

但现在只需更换将是一个巨大的帮助!再次感谢您对此的任何帮助。如果您有任何更多信息,请告诉我。

[Carrier]
Name=BT Customer Commitment
ChargeStructure=1 Year 50K


[ChargeBands]
Loc=Local Rate Calls
Nat=National Rate Calls
dq1=Directory Enquiries
dq2=Directory Enquiries
dq3=Directory Enquiries
dq4=Directory Enquiries
dq5=Directory Enquiries

[ChargeRates]
Loc,E="Economy",0,1000,60000,990,2800,Nearest,100
Loc,S="Standard",0,1000,60000,2450,2800,Nearest,100
Loc,W="Weekend",0,1000,60000,690,2800,Nearest,100
Nat,E="Economy",0,1000,60000,3190,2800,Nearest,100
Nat,S="Standard",0,1000,60000,6390,2800,Nearest,100


[DailyRates]
Loc,0=0000:E,0800:S,1800:E
Loc,1=0000:E,0800:S,1800:E
Loc,2=0000:E,0800:S,1800:E
Loc,3=0000:E,0800:S,1800:E
Loc,4=0000:E,0800:S,1800:E

[Destinations]
0="National",N,N
00="International",I,IV
0024195="Gabon Mobiles",I,IMT
0024197="Gabon Mobiles",I,IMT
00242="Congo",I,IS
090610="Premium Rate",M,n
0906100="C & W Premium Rate",M,n

这就是我使用C#代码。

private void button3_Click(object sender, EventArgs e)
{
   File.WriteAllText(txtnewtariff.Text, Regex.Replace(File.ReadAllText(txtcurrent.Text), "[00]", "+"));


}

3 个答案:

答案 0 :(得分:0)

更改正则表达式:

private void button3_Click(object sender, EventArgs e)
{
   File.WriteAllText(txtnewtariff.Text, Regex.Replace(File.ReadAllText(txtcurrent.Text), "^(0|00){1,2}", "+", RegexOptions.Multiline));
}

答案 1 :(得分:0)

我认为单个正则表达式可能很棘手。

我认为你最好只将你的替代品应用到你想要修改的部分:

var lines = File.ReadAllLines("path").ToList();
var regex = new Regex(@"^[0]{1,2}");
var startFrom = lines.IndexOf("[Destinations]");

//Start replacing from the index of "[Destinations]"
for (int i = startFrom; i < lines.Count; i++)
{
    //Assuming the ini section ends at an empty line - stop replacing
    if(lines[i].Trim() == string.Empty)
        break;

    lines[i] = regex.Replace(lines[i], "+");
}

File.WriteAllLines("path", lines);

答案 2 :(得分:0)

我现在工作了,谢谢你。

private void button3_Click(object sender, EventArgs e)
{
    //File.WriteAllText(txtnewtariff.Text, Regex.Replace(File.ReadAllText(txtcurrent.Text), "^(0|00){1,2}", "+", RegexOptions.Multiline));

    if (txtcurrent.Text.Trim().Length == 0)
    {
        MessageBox.Show("No Tariff Selected","Error On Conversion");
        return;
    }

    if (txtnewtariff.Text.Trim().Length == 0)
    {
        MessageBox.Show("Please Select a New Tariff File", "Error On Conversion");
        return;
    }

    if (txtctrycode.Text.Trim().Length == 0)
    {
        MessageBox.Show("Please enter an E164 Country Code", "Error On Conversion");
        return;
    }

    var lines = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex = new Regex(@"^[00]{2}");
    var startFrom = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines[i].Trim() == string.Empty)
            break;

        lines[i] = regex.Replace(lines[i], "+");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);

    var lines1 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex1 = new Regex(@"^[0]{1}");
    var startFrom1 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom1; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines1[i].Trim() == string.Empty)
            break;

        lines[i] = regex1.Replace(lines[i], "+" + txtctrycode.Text);
    }

    File.WriteAllLines(txtnewtariff.Text, lines);

    var lines2 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex2 = new Regex(@"^[1]");
    var startFrom2 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom2; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines2[i].Trim() == string.Empty)
            break;

        lines[i] = regex2.Replace(lines[i], "+" + txtctrycode.Text + "1");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);
    File.WriteAllLines(txtnewtariff.Text, lines);

    var lines3 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex3 = new Regex(@"^[2]");
    var startFrom3 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom3; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines3[i].Trim() == string.Empty)
            break;

        lines[i] = regex3.Replace(lines[i], "+" + txtctrycode.Text + "2");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);
    var lines4 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex4 = new Regex(@"^[3]");
    var startFrom4 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom4; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines4[i].Trim() == string.Empty)
            break;

        lines[i] = regex4.Replace(lines[i], "+" + txtctrycode.Text + "3");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);

    var lines5 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex5 = new Regex(@"^[4]");
    var startFrom5 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom5; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines5[i].Trim() == string.Empty)
            break;

        lines[i] = regex5.Replace(lines[i], "+" + txtctrycode.Text + "4");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);

    var lines6 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex6 = new Regex(@"^[5]");
    var startFrom6 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom6; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines6[i].Trim() == string.Empty)
            break;

        lines[i] = regex6.Replace(lines[i], "+" + txtctrycode.Text + "5");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);

    var lines7 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex7 = new Regex(@"^[6]");
    var startFrom7 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom7; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines7[i].Trim() == string.Empty)
            break;

        lines[i] = regex7.Replace(lines[i], "+" + txtctrycode.Text + "6");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);

    var lines8 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex8 = new Regex(@"^[7]");
    var startFrom8 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom8; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines8[i].Trim() == string.Empty)
            break;

        lines[i] = regex8.Replace(lines[i], "+" + txtctrycode.Text + "7");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);

    var lines9 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex9 = new Regex(@"^[8]");
    var startFrom9 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom9; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines9[i].Trim() == string.Empty)
            break;

        lines[i] = regex9.Replace(lines[i], "+" + txtctrycode.Text + "8");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);

    var lines10 = File.ReadAllLines(txtcurrent.Text).ToList();
    var regex10 = new Regex(@"^[9]");
    var startFrom10 = lines.IndexOf("[Destinations]");

    //Start replacing from the index of "[Destinations]"
    for (int i = startFrom10; i < lines.Count; i++)
    {
        //Assuming the ini section ends at an empty line - stop replacing
        if (lines10[i].Trim() == string.Empty)
            break;

        lines[i] = regex10.Replace(lines[i], "+" + txtctrycode.Text + "9");
    }

    File.WriteAllLines(txtnewtariff.Text, lines);

    MessageBox.Show(txtnewtariff.Text,
"Conversion Complete");