有一样式表,如下:
<style>
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
@page
{margin:1.0in .75in 1.0in .75in;
mso-header-margin:.5in;
mso-footer-margin:.5in;}
tr
{mso-height-source:auto;
mso-ruby-visibility:none;}
col
{mso-width-source:auto;
mso-ruby-visibility:none;}
br
{mso-data-placement:same-cell;}
.style0
{mso-number-format:General;
text-align:general;
vertical-align:middle;
white-space:nowrap;
mso-rotate:0;
mso-background-source:auto;
mso-pattern:auto;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
border:none;
mso-protection:locked visible;
mso-style-name:常规;
mso-style-id:0;}
td
{mso-style-parent:style0;
padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:general;
vertical-align:middle;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl24
{mso-style-parent:style0;
text-align:center;}
.xl25
{mso-style-parent:style0;
text-align:center;
border-top:.5pt solid windowtext;
border-right:none;
border-bottom:none;
border-left:.5pt solid windowtext;}
.xl26
{mso-style-parent:style0;
text-align:center;
border-top:.5pt solid windowtext;
border-right:none;
border-bottom:none;
border-left:none;}
.xl27
{mso-style-parent:style0;
text-align:center;
border-top:.5pt solid windowtext;
border-right:.5pt solid windowtext;
border-bottom:none;
border-left:none;}
.xl28
{mso-style-parent:style0;
text-align:center;
border-top:none;
border-right:none;
border-bottom:none;
border-left:.5pt solid windowtext;}
.xl29
{mso-style-parent:style0;
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:none;
border-left:none;}
.xl30
{mso-style-parent:style0;
text-align:center;
border-top:none;
border-right:none;
border-bottom:.5pt solid windowtext;
border-left:.5pt solid windowtext;}
.xl31
{mso-style-parent:style0;
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:none;}
.xl32
{mso-style-parent:style0;
text-align:center;
border-top:.5pt solid windowtext;
border-right:none;
border-bottom:.5pt solid windowtext;
border-left:.5pt solid windowtext;}
.xl33
{mso-style-parent:style0;
text-align:center;
border-top:.5pt solid windowtext;
border-right:none;
border-bottom:.5pt solid windowtext;
border-left:none;}
.xl34
{mso-style-parent:style0;
text-align:center;
border-top:.5pt solid windowtext;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:none;}
.xl35
{mso-style-parent:style0;
text-align:center;
border-top:.5pt solid windowtext;
border-right:.5pt solid windowtext;
border-bottom:none;
border-left:.5pt solid windowtext;}
.xl36
{mso-style-parent:style0;
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:none;
border-left:.5pt solid windowtext;}
.xl37
{mso-style-parent:style0;
text-align:center;
border-top:none;
border-right:.5pt solid windowtext;
border-bottom:.5pt solid windowtext;
border-left:.5pt solid windowtext;}
.xl38
{mso-style-parent:style0;
text-align:center;
border-top:none;
border-right:none;
border-bottom:.5pt solid windowtext;
border-left:none;}
.xl39
{mso-style-parent:style0;
text-align:center;
border:.5pt solid windowtext;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;
display:none;}
-->
</style>
需要把如下字符串提取出来,保存为一个数组(ArrayList也行)
@page
tr
col
br
.style0
td
.xl24
.xl25
.xl26
.xl27
.xl28
.xl29
.xl30
.xl31
.xl32
.xl33
.xl34
.xl35
.xl36
.xl37
.xl38
.xl39
ruby
rt
请教高手该怎么做?

解决方案 »

  1.   

    用 string.IndexOf() 或正则。
      

  2.   

    using System;
    using System.Collections;
    using System.Text.RegularExpressions;class Program  
    {
      static string style = @"<style> 
    <!--table 
    {mso-displayed-decimal-separator:""\.""; 
    mso-displayed-thousand-separator:""\,"";} 
    @page 
    {margin:1.0in .75in 1.0in .75in; 
    mso-header-margin:.5in; 
    mso-footer-margin:.5in;} 
    tr 
    {mso-height-source:auto; 
    mso-ruby-visibility:none;} 
    col 
    {mso-width-source:auto; 
    mso-ruby-visibility:none;} 
    br 
    {mso-data-placement:same-cell;} 
    .style0 
    {mso-number-format:General; 
    text-align:general; 
    vertical-align:middle; 
    white-space:nowrap; 
    mso-rotate:0; 
    mso-background-source:auto; 
    mso-pattern:auto; 
    color:windowtext; 
    font-size:12.0pt; 
    font-weight:400; 
    font-style:normal; 
    text-decoration:none; 
    font-family:宋体; 
    mso-generic-font-family:auto; 
    mso-font-charset:134; 
    border:none; 
    mso-protection:locked visible; 
    mso-style-name:常规; 
    mso-style-id:0;} 
    td 
    {mso-style-parent:style0; 
    padding-top:1px; 
    padding-right:1px; 
    padding-left:1px; 
    mso-ignore:padding; 
    color:windowtext; 
    font-size:12.0pt; 
    font-weight:400; 
    font-style:normal; 
    text-decoration:none; 
    font-family:宋体; 
    mso-generic-font-family:auto; 
    mso-font-charset:134; 
    mso-number-format:General; 
    text-align:general; 
    vertical-align:middle; 
    border:none; 
    mso-background-source:auto; 
    mso-pattern:auto; 
    mso-protection:locked visible; 
    white-space:nowrap; 
    mso-rotate:0;} 
    .xl24 
    {mso-style-parent:style0; 
    text-align:center;} 
    .xl25 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:.5pt solid windowtext; 
    border-right:none; 
    border-bottom:none; 
    border-left:.5pt solid windowtext;} 
    .xl26 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:.5pt solid windowtext; 
    border-right:none; 
    border-bottom:none; 
    border-left:none;} 
    .xl27 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:.5pt solid windowtext; 
    border-right:.5pt solid windowtext; 
    border-bottom:none; 
    border-left:none;} 
    .xl28 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:none; 
    border-right:none; 
    border-bottom:none; 
    border-left:.5pt solid windowtext;} 
    .xl29 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:none; 
    border-right:.5pt solid windowtext; 
    border-bottom:none; 
    border-left:none;} 
    .xl30 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:none; 
    border-right:none; 
    border-bottom:.5pt solid windowtext; 
    border-left:.5pt solid windowtext;} 
    .xl31 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:none; 
    border-right:.5pt solid windowtext; 
    border-bottom:.5pt solid windowtext; 
    border-left:none;} 
    .xl32 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:.5pt solid windowtext; 
    border-right:none; 
    border-bottom:.5pt solid windowtext; 
    border-left:.5pt solid windowtext;} 
    .xl33 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:.5pt solid windowtext; 
    border-right:none; 
    border-bottom:.5pt solid windowtext; 
    border-left:none;} 
    .xl34 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:.5pt solid windowtext; 
    border-right:.5pt solid windowtext; 
    border-bottom:.5pt solid windowtext; 
    border-left:none;} 
    .xl35 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:.5pt solid windowtext; 
    border-right:.5pt solid windowtext; 
    border-bottom:none; 
    border-left:.5pt solid windowtext;} 
    .xl36 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:none; 
    border-right:.5pt solid windowtext; 
    border-bottom:none; 
    border-left:.5pt solid windowtext;} 
    .xl37 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:none; 
    border-right:.5pt solid windowtext; 
    border-bottom:.5pt solid windowtext; 
    border-left:.5pt solid windowtext;} 
    .xl38 
    {mso-style-parent:style0; 
    text-align:center; 
    border-top:none; 
    border-right:none; 
    border-bottom:.5pt solid windowtext; 
    border-left:none;} 
    .xl39 
    {mso-style-parent:style0; 
    text-align:center; 
    border:.5pt solid windowtext;} 
    ruby 
    {ruby-align:left;} 
    rt 
    {color:windowtext; 
    font-size:9.0pt; 
    font-weight:400; 
    font-style:normal; 
    text-decoration:none; 
    font-family:宋体; 
    mso-generic-font-family:auto; 
    mso-font-charset:134; 
    mso-char-type:none; 
    display:none;} 
    --> 
    </style>";  static void Main()
      {
        // 生成:
        ArrayList a = new ArrayList();
        foreach (Match m in Regex.Matches(style, @"\}\s*(.+?)\s*\{"))
        {
          a.Add(m.Groups[1].Value);
        }
        
        // 输出:
        foreach (string s in a)
        {
          Console.WriteLine(s);
        }
      }
    }/* 程序输出:
    @page
    tr
    col
    br
    .style0
    td
    .xl24
    .xl25
    .xl26
    .xl27
    .xl28
    .xl29
    .xl30
    .xl31
    .xl32
    .xl33
    .xl34
    .xl35
    .xl36
    .xl37
    .xl38
    .xl39
    ruby
    rt
    */
      

  3.   

    如果要把结果保存到数组中,也很容易:    
    string[] stringArray = (string[])a.ToArray(typeof(string));
      static void Main()
      {
        // 生成:
        ArrayList a = new ArrayList();
        foreach (Match m in Regex.Matches(style, @"\}\s*(.+?)\s*\{"))
        {
          a.Add(m.Groups[1].Value);
        }
        string[] stringArray = (string[])a.ToArray(typeof(string));
        
        // 输出:
        foreach (string s in stringArray)
        {
          Console.WriteLine(s);
        }
      }
      

  4.   

    /*
    如果正则改为:@"\}\s*(\..+?)\s*\{"
    则输出为:
    .style0
    .xl24
    .xl25
    .xl26
    .xl27
    .xl28
    .xl29
    .xl30
    .xl31
    .xl32
    .xl33
    .xl34
    .xl35
    .xl36
    .xl37
    .xl38
    .xl39
    */