通过 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
...
我的代码(,工作):
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()); } } } } }