使用c# regex搜索文本

本文关键字:文本 搜索 regex 使用 | 更新日期: 2023-09-27 18:18:38

通过c# regex帮助查找所有"rect NAME = null"。(在globals和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
...

My code(, working):

  1. private void openFileDialog1_FileOk(对象发送者,CancelEventArgse){字符串startglobs = @"^'s*globals's*$";字符串endglobals = @"^'s*endglobals's*$";字符串currrect = @ ^ ' s *矩形' s + () ' s = ' s *空' 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());
                                }
                            }
                        }
                    }
                }
    

使用c# regex搜索文本

给你个提示…

你想要的正则表达式是

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

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

这已经给了你足够的时间在10分钟内解决你的问题,但是你必须自己做一些阅读…