我正在尝试编写一个正则表达式来提取下面的信息块,以及每个块中的字段。我正在使用Powershell。
我想捕获所有“服务器项”块,以及每个块的以下信息:
Server Item (1 **or more** of these items in the text)
Identity (1 **or more** of these Identity items per Server Item)
-- Allow (Each Identity contains **one** Allow)
-- Deny (Each Identity contains **one** Deny)
-- Allow (Inherited) (Each Identity contains **one** Allow (Inherited))
-- Deny (Inherited) (Each Identity contains **one** Deny (Inherited))
正如您所看到的,信息是分层的(对于其子项的每个标题,一对多)。
任何答案非常感谢!
示例输入文本,如下:
Server item: $/The/Path/Goes/Here
Identity: Identity Number One (TYPE A)
Allow:
Deny:
Allow (Inherited): Read, Write, Checkin, Label
Lock, CheckinOther
Deny (Inherited):
====================================================================
Server item: $/The/Other/Path/Goes/Here
Identity: Identity Number One (TYPE B)
Allow: Read, Write, Checkin, Label
Lock, CheckinOther
Deny:
Allow (Inherited):
Deny (Inherited):
====================================================================
等等
我尝试过以下内容:
$thePattern = @"
(?<serveritem>Server item:(.|\n)*?=)
"@
$myText -match $thePattern
这不会捕获所有项目,只给我第一个!另外,如何捕获每个服务器项目的身份和字段信息 - >身份 - >权限?
所需的输出将是捕获所有服务器项,并能够访问每个身份,并且对于每个身份,能够访问权限(允许,拒绝等)。目标是迭代块以便将信息添加到数据库以进行查询。
我正在通过以下修改来解决这个问题。
(?s)Server item:(?<serveritem>.*?)[\r\n]+ *Identity:(?<identity>.*?)[\r\n]+ *Allow: ?(?<allow>.*?)[\r\n]+ *Deny: ?(?<deny>.*?)[\r\n]+ *Allow \(Inherited\): ?(?<allowinherited>.*?)[\r\n]+ *Deny \(Inherited\): ?(?<denyinherited>.*?)([\r\n]+=|$)
假设(文本)输入的格式与样本一致,如果分解输入并以逐行方式迭代,则可以使用更简单的正则表达式提取所需的信息。
例如,给定以下输入“每个服务器项目中有一个或多个这些标识项”:
Server item: $/The/Path/Goes/Here
Identity: Identity Number One (TYPE A)
Allow:
Deny:
Allow (Inherited): Read, Write, Checkin, Label
Lock, CheckinOther
Deny (Inherited):
====================================================================
Server item: $/The/Other/Path/Goes/Here
Identity: Identity Number One (TYPE B)
Allow: Read, Write, Checkin, Label
Lock, CheckinOther
Deny:
Allow (Inherited):
Deny (Inherited):
====================================================================
Server item: $/The/Other/other/Path/Goes/Here
Identity: Identity Number One (TYPE C)
Allow: Read, Write, Checkin, Label
Lock, CheckinOther
Deny:
Allow (Inherited):
Deny (Inherited):
Identity: Identity Number One (TYPE D)
Allow: Read, Write, Checkin, Label
Lock, CheckinOther
Deny:
Allow (Inherited):
Deny (Inherited):
要获取分层信息:
# used .txt file for example
$lines = Get-Content $path;
$result = @{};
$serverItem = '';
$identityItem = '';
$currentKey = '';
foreach ($line in $lines) {
$key, $value = [Regex]::Split($line.Trim(), '\s*:\s*', 2);
switch -Regex ($key) {
'^server item' {
$serverItem = $value;
$result.$serverItem = @{};
continue;
}
'^identity' {
$identityItem = $value;
$result.$serverItem.$identityItem = @{};
continue;
}
'^[A-Za-z]+' {
if ($value -ne $null) {
$currentKey = $key;
$result.$serverItem.$identityItem.$key = $value;
} else {
$result.$serverItem.$identityItem.$currentKey += ", $key";
}
}
}
}
Server item:(.*?)[\r\n]+ *Identity:(.*?)[\r\n]+ *Allow: ?(.*?)[\r\n]+ *Deny: ?(.*?)[\r\n]+ *Allow \(Inherited\): ?(.*?)[\r\n]+ *Deny \(Inherited\): ?(.*?)([\r\n]+=|$)
选项/gs
(全球+单线)
匹配
Server item: $/The/Path/Goes/Here
Identity: Identity Number One (TYPE A)
Allow:
Deny:
Allow (Inherited): Read, Write, Checkin, Label
Lock, CheckinOther
Deny (Inherited):
====================================================================
Server item: $/The/Other/Path/Goes/Here
Identity: Identity Number One (TYPE B)
Allow: Read, Write, Checkin, Label
Lock, CheckinOther
Deny:
Allow (Inherited):
Deny (Inherited):
MATCH1
MATCH2
用regex101测试