用于匹配块中字段值的正则表达式 (tf.exe perm) TFS 2008

RegEx to match field values in a block (tf.exe perm) TFS 2008

我正在尝试编写一个正则表达式来提取下面的信息块,以及每个块中的字段。我正在使用 Powershell。

我想捕获所有 "Server Item" 个块,以及每个块的以下信息:

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))

如您所见,信息是分层的(每个标题到 children 一对多)。

非常感谢任何答案!

示例输入文本,如下:

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

这并没有捕获所有项目,只给了我第一个! 另外,如何捕获每个服务器项目的标识和字段信息 --> 标识 --> 权限?

所需的输出将是捕获所有服务器项目,并能够访问每个身份,并且对于每个身份,能够访问权限(允许、拒绝等)objective是遍历区块,将信息添加到数据库中查询。

我正在对此进行以下修改。

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):  

匹配 1

  • 第 1 组:$/The/Path/Goes/Here
  • 第 2 组:身份编号一(类型 a)
  • 第 3 组:[空]
  • 第 4 组:[空]
  • 第 5 组:读取、写入、签入、标签 [NEWLINE + SPACES] 锁定、签入其他
  • 第 6 组:[空]

匹配 2

  • 第 1 组:$/The/Other/Path/Goes/Here
  • 第 2 组:一号身份(b 型)
  • 第 3 组:读取、写入、签入、标签 [NEWLINE + SPACES] 锁定、签入其他
  • 第 4 组:[空]
  • 第 5 组:[空]
  • 第 6 组:[空]

使用 regex101

测试

假设(文本)输入的格式与您的示例一致,如果您分解输入并以逐行方式迭代,则可以使用更简单的正则表达式提取所需的信息。

例如,给定以下输入“每个服务器项 1 或更多 这些标识项”:

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";
            }
        }
    }
}