我有一个结构化的文件,带有描述Delphi(DFM文件)中的GUI的分层文本。正则表达式问题:在上下文匹配
让我们假设我有这个文件,我必须匹配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”,而不是“$” ......)。我需要把它放在一起,但忽略其他行,除了其他“对象xxx:yyy”和“结束”行....
我会很高兴有一些帮助!
可变长度lookbehind:多数民众赞成我试过的拳头......我想我需要做一些脚本。现在我将首先使用/^[] {2,} object \ w +:TMyButton \ r \ n(^ [] {4,}。+ \ r \ n)+^[] {2,}结束搜索上下文\ r \ n/mi,然后在此匹配中搜索/^[] {4,} Color = \ w + \ r \ n/mi – 2009-09-24 12:45:50