我有一个带有分层文本的结构化文件,它描述了 Delphi 中的 GUI(一个 DFM 文件)。
假设我有这个文件,我必须匹配 TmyButton(标记)上下文中的所有“Color = xxx”行,而不是其他上下文中的那些。在 TMyButton-Context 内不会有更深的层次结构。
object frmMain: TfrmMain
Left = 311
Top = 201
Color = clBtnFace
object MyFirstButton: TMyButton
Left = 555
Top = 301
Color = 16645072 <<<<<<MATCH THIS
OnClick = ButtonClick
end
object MyLabel: TLabel
Left = 362
Top = 224
Caption = 'a Caption'
Color = 16772831
Font.Color = clWindowText
end
object Panel2: TLTPanel
Left = 348
Top = 58
Width = 444
Height = 155
Color = clRed
object MyOtherButton: TMyButton
Left = 555
Top = 301
Color = 16645072 <<<<<<MATCH THIS
OnClick = ButtonClick
end
end
end
我试了两天,做了很多很多不同的尝试。这是我的一些不完整的模式:
/^[ ]{2,}object [A-Za-z0-9]+: TmyButton\r\n/mi <<<Matches the needed context
/^[ ]{4,}Color = [A-Za-z0-9]+\r\n/mi <<<Matches the needed result
/^[ ]{2,}end\r\n/mi <<<Matches the end of the context
(我不知道为什么,但我不得不使用“\r\n”而不是“$”......)。我需要把它放在一起,但忽略除其他“object xxx:yyy”和“end”行之外的其他行......
我很高兴能得到一些帮助!