Regex 正则表达式匹配块(tf.exe perm)TFS 2008中的字段值
我试图编写一个正则表达式来提取下面的信息块,以及每个块中的字段。我正在使用Powershell 我想捕获所有“服务器项”块,以及每个块的以下信息:Regex 正则表达式匹配块(tf.exe perm)TFS 2008中的字段值,regex,powershell,regex-group,tf-cli,Regex,Powershell,Regex Group,Tf Cli,我试图编写一个正则表达式来提取下面的信息块,以及每个块中的字段。我正在使用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)
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
$thePattern=@”
(?服务器项:(.|\n)*?=)
"@
$myText-匹配$thePattern
这并没有捕获所有的项目,只是给了我第一个!
另外,如何捕获每个服务器项-->标识-->权限的标识和字段信息
所需的输出将是捕获所有服务器项,并且能够访问每个标识,对于每个标识,能够访问权限(允许、拒绝等)。目标是遍历块,以便将信息添加到数据库中进行查询
我正在进行以下修改
- 这包括命名的捕获组李>
- 还要注意使用(?s)设置单行选项
- 由于powershell/.net不支持全局选项,我使用了[Regex]::Matches来匹配所有选项
(?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]+=|$)
(?s)服务器项:(?*?[\r\n]+*标识:(?*?)[\r\n]+*允许:?(?*??)[\r\n]+*拒绝:?(?*?*?)[\r\n]+*允许\(继承\):(?*?):(?(?*?)[\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):
匹配1
- 第一组:$/The/Path/Goes/Here
- 第2组:身份编号1(a类)
- 第3组:[空]
- 第4组:[空]
- 第5组:读、写、签入、标记[换行符+空格]锁、签入其他
- 第6组:[空]
- 第一组:$/The/Other/Path/Goes/Here
- 第2组:身份号码1(b类)
- 第3组:读、写、签入、标记[NEWLINE+SPACES]锁、签入其他
- 第4组:[空]
- 第5组:[空]
- 第6组:[空]
# 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";
}
}
}
}
你期望的输出是什么?您需要一个正则表达式来应用于每一行还是一个正则表达式来同时应用于所有行?我正在寻找一个正则表达式来同时应用于所有行。我希望遍历匹配,以及每个匹配中的捕获组。“外部”匹配项将与服务器项匹配。这能做到吗?推荐的替代品?非常好,谢谢。您的答案似乎不符合服务器项级别。@Banoona我添加了服务器项。我想这可能是其他项目的说明,因为你的问题中“身份”前面有一个点谢谢克里斯。这对我来说是一个很好的开始,我可以把这件事做到底。我对您的解决方案进行了一些修改,为我提供了命名的捕获组:(?)服务器项:(.*)[\r\n]+标识:(?)[\r\n]+允许:(?)[\r\n]+拒绝:(?)[\r\n]+允许(继承):(\r\n]+拒绝(继承):(?)([\r\n]+=$)但是,我没有完全获得我需要的层次结构,即一个具有许多标识的服务器项,并且每个服务器项都有许多权限集(允许、拒绝、允许(继承)和拒绝(继承)。我使用的是powershell(.Net),因此将使用类似([Regex]$thematchPattern)。Matches($theRawContent)[1]。Groups[“identity”]-->并希望捕获子组。可以这样做吗?如果不能,答案很合适,因为我可以迭代外部匹配(服务器项),然后使用另一个表达式为每个子项(标识和权限)重新匹配@Banoona这变得非常复杂。正则表达式现在已经非常长了,我不知道多个标识项是如何链接的。我建议使用kuujinbos解决方案这样的方法来避免复杂性。没有人能够阅读正则表达式并理解它现在的功能。
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";
}
}
}
}