1

嗨,我正在学习 C#,我有一些 .inf 文件(请参见下面的示例),我需要能够更改 [Destinations] 部分,其中一行以“00”或“0”开头并带有“+”。我曾尝试使用 Regex.Replace,但它会替换文件中包含“00”的所有条目。在这一点上,将两个条目都保留在文件中会很好但不是必需的。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]", "+"));


}
4

3 回答 3

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));
}
于 2013-04-23T10:41:51.857 回答
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);
于 2013-04-23T10:42:30.247 回答
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");
于 2013-04-23T16:15:15.243 回答