解决方案 »

  1.   

    再来个复杂点的--平衡组
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    namespace sxLdfang
    {
        class Program
        {
            static void Main(string[] args)
            {
                string html = @"s=oEditor.CreateBox(
    [
        ""NAME:BoxParameters"",
        ""XPosition:=""  , ""-1.4mm"",
        ""YPosition:=""  , ""1.4mm"",
        ""ZPosition:=""  , ""0mm"",
        ""XSize:=""  , ""0.6mm"",
        ""YSize:=""  , ""1.4mm"",
        ""ZSize:=""  , ""-0.6mm""
    ], 
    [
        ""NAME:Attributes"",
        ""Name:=""  , ""Box1"",
        ""Flags:=""  , """",
        ""Color:=""  , ""(132 132 193)"",
        ""Transparency:="" , 0,
        ""PartCoordinateSystem:="", ""Global"",
        ""UDMId:=""  , """",
        ""MaterialValue:="" , ""\""vacuum\"""",
        ""SolveInside:=""  , True
    ])
    oEditor.ChangeProperty(
    [
        ""NAME:AllTabs"",
        [
            ""NAME:Geometry3DCmdTab"",
            [
                ""NAME:PropServers"", 
                ""Box1:CreateBox:1""
            ],
            [
                ""NAME:ChangedProps"",
                [
                    ""NAME:XSize"",
                    ""Value:=""  , ""2mm""
                ],
                [
                    ""NAME:YSize"",
                    ""Value:=""  , ""3mm""
                ]
            ]
        ]
    ])
    oEditor.CreateCylinder(
    [
        ""NAME:CylinderParameters"",
        ""XCenter:=""  , ""0mm"",
        ""YCenter:=""  , ""-2.2mm"",
        ""ZCenter:=""  , ""0mm"",
        ""Radius:=""  , ""0.824621125123532mm"",
        ""Height:=""  , ""-1mm"",
        ""WhichAxis:=""  , ""Z"",
        ""NumSides:=""  , ""0""
    ], 
    [
        ""NAME:Attributes"",
        ""Name:=""  , ""Cylinder1"",
        ""Flags:=""  , """",
        ""Color:=""  , ""(132 132 193)"",
        ""Transparency:="" , 0,
        ""PartCoordinateSystem:="", ""Global"",
        ""UDMId:=""  , """",
        ""MaterialValue:="" , ""\""vacuum\"""",
        ""SolveInside:=""  , True
    ])
    oEditor.ChangeProperty(
    [
        ""NAME:AllTabs"",
        [
            ""NAME:Geometry3DCmdTab"",
            [
                ""NAME:PropServers"", 
                ""Box1:CreateBox:1""
            ],
            [
                ""NAME:ChangedProps"",
                [
                    ""NAME:XSize"",
                    ""Value:=""  , ""1mm""
                ]
            ]
        ]
    ])";
                string pattern = @"(?isnx) #匹配模式,忽略大小写,“.”匹配任意字符
    \[ #开始标记“[”
        (?> #分组构造,用来限定量词“*”修饰范围
            \[(?<Open>) #遇到开始标记,入栈,Open计数加1
              | #分支结构
            \](?<-Open>) #狭义平衡组,遇到结束标记,Open计数减1
              | #分支结构
            ((?![\[\]]).)* #右侧不为开始或结束标记的任意字符
        )* #以上子串出现0次或任意多次
        (?(Open)(?!)) #还有'OPEN',有则说明不配对,什么都不匹配
    \] #结束标记“]”";
                MatchCollection mc = Regex.Matches(html, pattern);
                foreach (Match m in mc)
                {
                    Console.WriteLine("\n\n----------------------------------------------------------------------------");
                    Console.WriteLine( m.Value);
                }
                Console.ReadKey();
            }
        }
    }运行结果:
    ----------------------------------------------------------------------------
    [
        "NAME:BoxParameters",
        "XPosition:="        , "-1.4mm",
        "YPosition:="        , "1.4mm",
        "ZPosition:="        , "0mm",
        "XSize:="    , "0.6mm",
        "YSize:="    , "1.4mm",
        "ZSize:="    , "-0.6mm"
    ]
    ----------------------------------------------------------------------------
    [
        "NAME:Attributes",
        "Name:="     , "Box1",
        "Flags:="    , "",
        "Color:="    , "(132 132 193)",
        "Transparency:="    , 0,
        "PartCoordinateSystem:=", "Global",
        "UDMId:="    , "",
        "MaterialValue:="   , "\"vacuum\"",
        "SolveInside:="      , True
    ]
    ----------------------------------------------------------------------------
    [
        "NAME:AllTabs",
        [
            "NAME:Geometry3DCmdTab",
            [
                "NAME:PropServers",
                "Box1:CreateBox:1"
            ],
            [
                "NAME:ChangedProps",
                [
                    "NAME:XSize",
                    "Value:="        , "2mm"
                ],
                [
                    "NAME:YSize",
                    "Value:="        , "3mm"
                ]
            ]
        ]
    ]
    ----------------------------------------------------------------------------
    [
        "NAME:CylinderParameters",
        "XCenter:="  , "0mm",
        "YCenter:="  , "-2.2mm",
        "ZCenter:="  , "0mm",
        "Radius:="   , "0.824621125123532mm",
        "Height:="   , "-1mm",
        "WhichAxis:="        , "Z",
        "NumSides:="         , "0"
    ]
    ----------------------------------------------------------------------------
    [
        "NAME:Attributes",
        "Name:="     , "Cylinder1",
        "Flags:="    , "",
        "Color:="    , "(132 132 193)",
        "Transparency:="    , 0,
        "PartCoordinateSystem:=", "Global",
        "UDMId:="    , "",
        "MaterialValue:="   , "\"vacuum\"",
        "SolveInside:="      , True
    ]
    ----------------------------------------------------------------------------
    [
        "NAME:AllTabs",
        [
            "NAME:Geometry3DCmdTab",
            [
                "NAME:PropServers",
                "Box1:CreateBox:1"
            ],
            [
                "NAME:ChangedProps",
                [
                    "NAME:XSize",
                    "Value:="        , "1mm"
                ]
            ]
        ]
    ]
      

  2.   

    谢谢楼上两位回答。现在问题来了,我同事说在
    string html = @"s=oEditor.CreateBox(
    [
        ""NAME:BoxParameters"",
        ""XPosition:=""     , ""-1.4mm"",
        ""YPosition:=""     , ""1.4mm"",
        ""ZPosition:=""     , ""0mm"",
        ""XSize:=""     , ""0.6mm"",
        ""YSize:=""     , ""1.4mm"",
        ""ZSize:=""     , ""-0.6mm""
    ], 
    [
        ""NAME:Attributes"",
        ""Name:=""     , ""Box1"",
        ""Flags:=""     , """",
        ""Color:=""     , ""(132 132 193)"",
        ""Transparency:=""    , 0,
        ""PartCoordinateSystem:="", ""Global"",
        ""UDMId:=""     , """",
        ""MaterialValue:=""    , ""\""vacuum\"""",
        ""SolveInside:=""     , True
    ])
      

  3.   

    对于如下字符串
    string html = @"s=oEditor.CreateBox(
    [
        ""NAME:BoxParameters"",
        ""XPosition:=""     , ""-1.4mm"",
        ""YPosition:=""     , ""1.4mm"",
        ""ZPosition:=""     , ""0mm"",
        ""XSize:=""     , ""0.6mm"",
        ""YSize:=""     , ""1.4mm"",
        ""ZSize:=""     , ""-0.6mm""
    ], 
    [
        ""NAME:Attributes"",
        ""Name:=""     , ""Box1"",
        ""Flags:=""     , """",
        ""Color:=""     , ""(132 132 193)"",
        ""Transparency:=""    , 0,
        ""PartCoordinateSystem:="", ""Global"",
        ""UDMId:=""     , """",
        ""MaterialValue:=""    , ""\""vacuum\"""",
        ""SolveInside:=""     , True
    ])
    我需要把里面的XPosition等属性及对应的数值给读出来。
    我可以通过如下匹配项
    oEditor.CreateBox\(
    \s*\[
    \s*"NAME:BoxParameters",
    \s*"(?<XPosition>XPosition):=" , "(?<XPositionValue>-?\d.?(\d?)+mm)",
    \s*"(?<YPosition>YPosition):=" , "(?<YPositionValue>-?\d.?(\d?)+mm)",
    \s*"(?<ZPosition>ZPosition):=" , "(?<ZPositionValue>-?\d.?(\d?)+mm)",
    \s*"(?<XSize>XSize):=" , "(?<XSizeValue>-?\d.?(\d?)+mm)",
    \s*"(?<YSize>YSize):=" , "(?<YSizeValue>-?\d.?(\d?)+mm)",
    \s*"(?<ZSize>ZSize):=" , "(?<ZSizeValue>-?\d.?(\d?)+mm)"
    \s*\],
    能把前一个中括号的内容给读书来,并通过组的形式获取到各个属性及值。但是我用
    oEditor.CreateBox\(
    \s*\[
    \s*"NAME:BoxParameters",
    \s*"(?<XPosition>XPosition):=" , "(?<XPositionValue>-?\d.?(\d?)+mm)",
    \s*"(?<YPosition>YPosition):=" , "(?<YPositionValue>-?\d.?(\d?)+mm)",
    \s*"(?<ZPosition>ZPosition):=" , "(?<ZPositionValue>-?\d.?(\d?)+mm)",
    \s*"(?<XSize>XSize):=" , "(?<XSizeValue>-?\d.?(\d?)+mm)",
    \s*"(?<YSize>YSize):=" , "(?<YSizeValue>-?\d.?(\d?)+mm)",
    \s*"(?<ZSize>ZSize):=" , "(?<ZSizeValue>-?\d.?(\d?)+mm)"
    \s*\],
    \s*\[
    \s*\"NAME:Attributes",
    \s*\"Name:=" , "Box1",
    \s*\"Flags:=" , "",
    \s*\"Color:=" , "(132 132 193)",
    \s*\"Transparency:=" , 0,
    \s*\"PartCoordinateSystem:=", "Global",
    \s*\"UDMId:=" , "",
    \s*\"MaterialValue:=" , "\"vacuum\"",
    \s*\"SolveInside:=" , True
    \s*\]\)
    却无法读出整个oEditor.CreateBox对象的所有内容(匹配项也是从文本读取的)。
    即使是用
    [code=csharpoEditor.CreateBox\(
    \s*\[
    \s*"NAME:BoxParameters",
    \s*"(?<XPosition>XPosition):=" , "(?<XPositionValue>-?\d.?(\d?)+mm)",
    \s*"(?<YPosition>YPosition):=" , "(?<YPositionValue>-?\d.?(\d?)+mm)",
    \s*"(?<ZPosition>ZPosition):=" , "(?<ZPositionValue>-?\d.?(\d?)+mm)",
    \s*"(?<XSize>XSize):=" , "(?<XSizeValue>-?\d.?(\d?)+mm)",
    \s*"(?<YSize>YSize):=" , "(?<YSizeValue>-?\d.?(\d?)+mm)",
    \s*"(?<ZSize>ZSize):=" , "(?<ZSizeValue>-?\d.?(\d?)+mm)"
    \s*\],
    \s*\[][/code]
    都无法读出第二个中括号的左括号!!
    我猜是不是因为正则表达式在空格和换行符方面有一些注意事项我没考虑到?求大神解答~