-3

通过 c# regex帮助查找所有“rect NAME = null”。(在全局变量和 endglobals 之间)

    //Text example:
...
         globals
            ...
        boolexpr cj_true_bool_4896bnao87
        string udg_globals = "endglobals"
        trigger gg_trg___________________________u=null
        rect gg_rct_MyReg1=null
        rect     ra2462346  =            null
            ...
          endglobals
...

我的代码(,工作):

  1. private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { string startglobs = @"^\s*globals\s*$"; 字符串 endglobs = @"^\s*endglobals\s*$"; 字符串 currrect = @"^\s*rect\s+(. )\s =\s*null\s*";

                    using (StreamReader file = new StreamReader(openFileDialog1.FileName))
                    {
                        string currline;
                        bool globalstate = false;
                        while ((currline = file.ReadLine()) != null)
                        {
                            /* find globals */
                            Regex startr = new Regex(startglobs);
                            Match startm = startr.Match(currline);
                            if (startm.Success)
                                globalstate = true;
    
    
                            /* find endglobls */
                            Regex endr = new Regex(endglobs);
                            Match endm = endr.Match(currline);
                            if (endm.Success)
                                globalstate = false;
    
                            /* if opened globals find global rect */
                            if (globalstate)
                            {
                                Regex foundrectr = new Regex(currrect);
                                Match foundrectm = foundrectr.Match(currline);
                                if (foundrectm.Success)
                                {
                                    MessageBox.Show(foundrectm.Groups[1].ToString());
                                }
                            }
                        }
    
                    }
    
                }
    
4

1 回答 1

1

我给你个提示...

你想要的正则表达式是

rect (.*)\s?=\s?null

您现在需要学习如何使用 regex 类来运行它,并且您可能会发现 MatchCollections 和 Groups 很有趣。

现在已经足够你在 10 分钟内解决你的问题,但你必须自己做一些阅读......

于 2013-06-17T12:06:48.993 回答