替换的文本位置
 1 12  9 17 22  0  0.0 2.342900261283D-04
    1.500000000000D+01 2.828125000000D+01
    5.904180000000D+05 2.000000000000D+00把所有这个位置的文本替换为红色字体,下面同理
 2 12  3 18  4  0  0.0 3.832229413092D-04
    2.000000000000D+00 0.000000000000D+00
    7.218000000000D+03 3.000000000000D+00
 3 12  3 18 12  0  0.0 2.402812242508D-05
    4.300000000000D+01-8.521875000000D+01
    3.847200000000D+04 2.000000000000D+00
 4 09  3 18  4  0  0.0 1.261560246348D-04
    7.400000000000D+01 3.546875000000D+01
    7.218000000000D+03 3.000000000000D+00
 9 12  3 16 10  0  0.0 1.455144956708D-04
    3.600000000000D+01 7.240625000000D+01
    2.881800000000D+04 4.000000000000D+00
10 12  3 18  4  0  0.0-3.260467201471D-05
    7.200000000000D+01 1.062500000000D+00
    7.218000000000D+03 5.000000000000D+00
51 12  3 17 22  0  0.0-2.426193095744D-04
    5.700000000000D+01 8.593750000000D+00
    5.904180000000D+05 0.000000000000D+00
12 12  3 19 10  0  0.0 5.850056186318D-05
    8.000000000000D+01-1.128125000000D+01
    2.881800000000D+04 6.000000000000D+00
13 12  3 19 12  0  0.0 2.187765203416D-04
    3.200000000000D+01 2.437500000000D+00
    4.258200000000D+04 5.000000000000D+00
15 12  3 18  8  0  0.0-9.192666038871D-05
    4.300000000000D+01-1.178125000000D+01
    2.161800000000D+04 0.000000000000D+00

解决方案 »

  1.   


      string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));//读取tx
                    string pattern = @"(?i)(?<=\d+\s+)((\d+\s+){4})(\d+\s+\d+\.\d+\s*?(-?\d+\.[^+-]+?[+-]\d{2,}\s*?){4})(-?\d+\.[^+-]+?[+-]\d{2,})(?=\s)";
                   
                    //替换
                    string begin_red = "<font color='Red'>";
                    string end_red = "</font>";
                    tempStr = Regex.Replace(tempStr, pattern, begin_red + "$1" + end_red + "$3" + begin_red + "$5" + end_red);
                    /*
                     1 <font color='Red'>12 9 17 22 </font>0 0.0 2.342900261283D-04
                      1.500000000000D+01 2.828125000000D+01
                      5.904180000000D+05 <font color='Red'>2.000000000000D+00</font>
                     2 <font color='Red'>12 3 18 4 </font>0 0.0 3.832229413092D-04
                      2.000000000000D+00 0.000000000000D+00
                      7.218000000000D+03 <font color='Red'>3.000000000000D+00</font>
                     3 <font color='Red'>12 3 18 12 </font>0 0.0 2.402812242508D-05
                      4.300000000000D+01-8.521875000000D+01
                      3.847200000000D+04 <font color='Red'>2.000000000000D+00</font>
                     4 <font color='Red'>09 3 18 4 </font>0 0.0 1.261560246348D-04
                      7.400000000000D+01 3.546875000000D+01
                      7.218000000000D+03 <font color='Red'>3.000000000000D+00</font>
                     9 <font color='Red'>12 3 16 10 </font>0 0.0 1.455144956708D-04
                      3.600000000000D+01 7.240625000000D+01
                      2.881800000000D+04 <font color='Red'>4.000000000000D+00</font>
                    10 <font color='Red'>12 3 18 4 </font>0 0.0-3.260467201471D-05
                      7.200000000000D+01 1.062500000000D+00
                      7.218000000000D+03 <font color='Red'>5.000000000000D+00</font>
                    51 <font color='Red'>12 3 17 22 </font>0 0.0-2.426193095744D-04
                      5.700000000000D+01 8.593750000000D+00
                      5.904180000000D+05 <font color='Red'>0.000000000000D+00</font>
                    12 <font color='Red'>12 3 19 10 </font>0 0.0 5.850056186318D-05
                      8.000000000000D+01-1.128125000000D+01
                      2.881800000000D+04 <font color='Red'>6.000000000000D+00</font>
                    13 <font color='Red'>12 3 19 12 </font>0 0.0 2.187765203416D-04
                      3.200000000000D+01 2.437500000000D+00
                      4.258200000000D+04 <font color='Red'>5.000000000000D+00</font>
                    15 <font color='Red'>12 3 18 8 </font>0 0.0-9.192666038871D-05
                      4.300000000000D+01-1.178125000000D+01
                      2.161800000000D+04 <font color='Red'>0.000000000000D+00</font>
                     */