Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用大括号和关键字之间的正则表达式获取内容_Regex_Powershell_Powershell 4.0 - Fatal编程技术网

Regex 使用大括号和关键字之间的正则表达式获取内容

Regex 使用大括号和关键字之间的正则表达式获取内容,regex,powershell,powershell-4.0,Regex,Powershell,Powershell 4.0,我正面临一个我无法解决的问题。 我试图建立我的正则表达式模式,它必须在大括号之间获取内容,但是如果只有一个确切的关键字位于打开的大括号之前 (?<=(?:\testlist\b)|(?:){)((.*?)(?=})) 使用上面的模式,我可以获取每个节点的内容,但我想在regex中指定只需要抓取“testlist”节点的内容。 (大括号的位置因设计不同而有所不同,因为我希望获得内容,即使大括号与关键字在同一行中,或者不管后面包含多少换行符) 有没有人知道,我怎样才能做到这一点 谢谢大家!

我正面临一个我无法解决的问题。 我试图建立我的正则表达式模式,它必须在大括号之间获取内容,但是如果只有一个确切的关键字位于打开的大括号之前

(?<=(?:\testlist\b)|(?:){)((.*?)(?=}))
使用上面的模式,我可以获取每个节点的内容,但我想在regex中指定只需要抓取“testlist”节点的内容。 (大括号的位置因设计不同而有所不同,因为我希望获得内容,即使大括号与关键字在同一行中,或者不管后面包含多少换行符)

有没有人知道,我怎样才能做到这一点


谢谢大家!

您可以使用类似正则表达式的

(?s)testlist\s*{(.*?)}
这与
testlist
字面匹配,后跟空格和一个文字大括号<代码>(.*)捕获所有内容,直到下一个右括号结束

用法:

PS C:\Users\greg> 'nodelist{
>> ...
>> ...
>> }
>> testlist
>> {
>> ...
>> ...
>> }' -match '(?s)testlist\s*{(.*?)}'
True
PS C:\Users\greg> $Matches.0
testlist
{
...
...
}
PS C:\Users\greg> $Matches.1

...
...

PS C:\Users\greg>   
如果您想要完全匹配而不是捕获组:

PS C:\Users\greg> 'nodelist{
>> ...
>> ...
>> }
>> testlist
>> {
>> ...
>> ...
>> }' -match '(?s)(?<=testlist\s*{).*?(?=})'
True
PS C:\Users\greg> $Matches.0

...
...

PS C:\Users\greg>   
PS C:\Users\greg>“节点列表”{
>> ...
>> ...
>> }
>>测试列表
>> {
>> ...
>> ...

>>}'-match'(?s)(?什么语言/引擎/平台?目前Windows上的Powershell 4即使它不作为完全匹配返回内容,但作为组匹配返回内容,这是一个非常简单和简短的解决方案,我可以使用。谢谢!获得完全匹配的问题是lookbehind是可变长度的,这是许多正则表达式引擎不支持的。Powershell支持,因此我添加了一个替代版本。请参阅编辑。谢谢,我对这个答案和解决方案完全满意!
PS C:\Users\greg> 'nodelist{
>> ...
>> ...
>> }
>> testlist
>> {
>> ...
>> ...
>> }' -match '(?s)(?<=testlist\s*{).*?(?=})'
True
PS C:\Users\greg> $Matches.0

...
...

PS C:\Users\greg>