0

我有以下文本,其中包含多个具有不同值的重复部分:

Section
-------
Property1: Val-a
Property2: 

 <dict>
 <key>key</key>
 <data>my data</data> 
 </dict>

Property3: Val-123

Section
-------
Property1: Val-c
Property2: Val-d
Property3: Val-4

Section
-------
Property1: Val-e
Property2: Val-f
Property3: Val-f

Section
-------
Property1: Val-gg
Property2: Val-d
Property3: Val-h

Section
-------
Property1: Val-x 
Property2: Val-f
Property3: Val-9

我想获取所有具有 Property2: Val-d 的部分。我想获得具有所有属性和值的整个部分。在上面的示例中,我想以第二和第四部分结尾:

Section
-------
Property1: Val-c
Property2: Val-d
Property3: Val-4

Section
-------
Property1: Val-gg
Property2: Val-d
Property3: Val-h

我怎样才能做到这一点?

4

2 回答 2

1

您可以在MULTILINE模式下使用此正则表达式:

(?m)^Section(?:\n[^\n]+)+?\nProperty2: Val-d(?:\n[^\n]+)*

正则表达式演示

于 2019-03-13T06:43:01.863 回答
1

可以进一步优化:

^Section
(?:(?!^Section)[\s\S])+?
^Property2:\ Val-d
(?:(?!^Section)[\s\S])+?
(?=^Section|\Z)

在 regex101.com 上查看演示

于 2019-03-13T06:40:57.113 回答