1

こんにちは、C# を学習しています。いくつかの .inf ファイルがあります (以下のサンプルを参照)。行が '00' または '0' で始まり、'+' で始まる [Destinations] セクションを変更できるようにする必要があります。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));

    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 に答える