有如下一个字符串,现想将其分开:
width = "777" height = "100%" style="background-attachment:  fixed center"分开后的格式为两个组成部分:
width  777
height 100%
style  background-attachment:  fixed center
应该怎么做呢?谢谢.

解决方案 »

  1.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    Regex r1 = new Regex(@"\b(\w+)\b *= *""([^""]+)""", RegexOptions.IgnoreCase);
    Regex r2 = new Regex(@"\b([\w-]+)\b *: *([^;]+)", RegexOptions.IgnoreCase); string input = this.textBox1.Text; MatchCollection mc1 = r1.Matches(input); foreach(Match m1 in mc1)
    {
    GroupCollection gc1 = m1.Groups;
    if(gc1.Count == 3)
    {
    if(gc1[1].Value.ToLower() == "style")
    {
    MatchCollection mc2 = r2.Matches(gc1[2].Value);
    foreach(Match m2 in mc2)
    {
    GroupCollection gc2 = m2.Groups;
    if(gc2.Count == 3)
    {
    this.textBox2.AppendText(gc2[1].Value + ":" + gc2[2].Value + System.Environment.NewLine);
    }
    }
    }
    else
    this.textBox2.AppendText(gc1[1].Value + ":" + gc1[2].Value + System.Environment.NewLine);
    }
    }
    }
      

  2.   

    string regexString=@"(\S*?)\s*?=\s*?""(.*?)""";
    Regex matchesRegex=new Regex(regexString);
    MatchCollection matchFound=matchesRegex.Matches("width = \"777\" height = \"100%\" style=\"background-attachment:  fixed center\"");
    if (matchFound.Count >0) {
    StringBuilder resultString=new StringBuilder(); 
    for (int i=0;i<matchFound.Count;i++ ) {
    for (int y=0;y<matchFound[i].Groups.Count;y++) {
    resultString.Append(i.ToString()+","+y.ToString()+")"+matchFound[i].Groups[y].Value +"\r\n"); 
    }
    resultString.Append("\r\n");
    }
    MessageBox.Show(resultString.ToString());  
    }
      

  3.   

    MessageBox.Show得到的结果如下:
    0,0)width = "777"
    0,1)width
    0,2)7771,0)height = "100%"
    1,1)height
    1,2)100%2,0)style="background-attachment:  fixed center"
    2,1)style
    2,2)background-attachment:  fixed center
      

  4.   

    string regexString=@"(\S*?)\s*?=\s*?""(.*?)""";
    谁能解释一下吗?谢谢。
      

  5.   

    (\S*?)\s*?=\s*?"(.*?)"这就是正则表达式!因为引号的转义问题,所以就变成了@"(\S*?)\s*?=\s*?""(.*?)"""
      

  6.   

    using System;
    using System.Text.RegularExpressions;
    class Print{   public static void Main(string[] args)
    {
    string msg = "width = \"777\" height = \"100%\" style=\"background-attachment:  fixed center\"";
    string regex = @"\s*(?<property>\w+)\b\s*=\s*""(?<value>[^""]*)""";

    RegexOptions options = (RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline 
    | RegexOptions.IgnoreCase | RegexOptions.Compiled);
    Regex reg = new Regex(regex, options);
    Console.WriteLine("The Soruce : " + msg);
    foreach(Match mc in reg.Matches(msg))
    {
    Console.WriteLine(mc.Groups["property"].Value + " = " + mc.Groups["value"].Value);
    }
    }
    }
      

  7.   

    解释正则表达式?这我不擅长!我尽力吧!(\S*?)\s*?=\s*?"(.*?)"\S       与任何非空白字符匹配。
    *        指定零个或更多个匹配。
    *后加?  指定尽可能少地使用重复的第一个匹配。
    ()     用于分组,捕获匹配的子字符串。
    \s       与任何空白字符匹配。如空格符
    .        与除 \n 之外的任何字符匹配。  (\S*?)\s*?=\s*?"(.*?)"
    这个表示:
    (零个或多个非空白字符),用于分组
    后跟零个或多个空格
    后跟=
    后跟零个或多个空格
    后跟"
    后跟(零个或多个非\n 之外的任何字符),用于分组
    后跟" 
    ^_^ 不知道我讲明白了没有?