回复 20# flashercs
大佬再请教一下,
标签前的符号, 前面的例子都是┋, 刚发现有时还可能会是┇ 还没有发现同时存在的情况,
我改了下面地方,想实现可以适用这两种符号, 结果标签输出为空:
$reLabel = [regex]'\s(┋|┇)([^,]*),(\s*)' #标签正则
和
if ($_.trim().StartsWith("┋") -or $_.trim().StartsWith("┇")) {
___________________________________________________________________- $A = @'
- ┇城市, 北京, 上海, 广州
- ┇大理, 三亚, 海南
- 我的名字是 ┃名字┃, 我的国家是 ┃国家┃, 我的年龄是┃年龄┃, 我家住在┃城市┃!
- '@
-
- [Collections.ArrayList]$B = @() #存标签
- [Collections.ArrayList]$C = @() #存内容
-
- [Collections.ArrayList]$CLocal = @() #存内容Local
- [Collections.ArrayList]$keysInContent = @() #keys in content
- $dicLabelCtr = @{} #标签个数统计
- # $dicLabelLocal = @{} #标签个数统计: {称呼 =称呼2}
- $dicLabelLocal4B = [ordered]@{} #{标签 = Line}
- #$reLabel = [regex]'┋([^,]*),(\s*)' #标签正则
- $reLabel = [regex]'\s(┋|┇)([^,]*),(\s*)' #标签正则
- $reContent = [regex]'┃([^┃]*)┃' #内容正则
- # 替换label
- $sbLabel = {
- param ([System.Text.RegularExpressions.Match]$m)
- $k = $m.Groups[1].Value
- if ($dicLabelCtr.ContainsKey($k))
- {
- $ctr = (++$dicLabelCtr[$k])
- "┃Y_$k$ctr┃ $k$ctr[]"
- }
- else
- {
- $dicLabelCtr[$k] = 1
- "┃Y_$k┃ $k[]"
- }
- }
- # 替换内容
- $sbContent = {
- param([System.Text.RegularExpressions.Match]$m)
- $k = $m.Groups[1].Value
- if ($dicLabelLocal4B.Contains($k)) {
- [void]$keysInContent.Add($k)
- if ($dicLabelCtr.ContainsKey($k)) {
- "┃Y_${k}$($dicLabelCtr[$k]+1)┃"
- } else {
- "┃Y_${k}┃"
- }
- } else {
- "┃N_$k┃"
- }
- }
-
- $stack = 0 #content
- $A -split '\r\n' | ForEach-Object {
- if ($stack -eq 0) {
- if ($_.trim().StartsWith("┋") -or $_.trim().StartsWith("┇")) {
- $stack = 1
- # procossing prev section
- for ($i = 0; $i -lt $CLocal.Count; $i++) {
- $CLocal[$i] = $reContent.Replace($CLocal[$i], $sbContent)
- }
- foreach ($key in @($dicLabelLocal4B.Keys)) {
- if (-not $keysInContent.Contains($key)) {
- [void]$C.Add($dicLabelLocal4B[$key])
- $dicLabelLocal4B.Remove($key)
- } else {
- [void]$B.Add(($reLabel.Replace($dicLabelLocal4B[$key], $sbLabel) -replace ',\s*', '[]'))
- }
- }
- [void]$C.AddRange($CLocal)
- $CLocal.Clear()
- $keysInContent.Clear()
- $dicLabelLocal4B.Clear()
- # next section
- $m = $reLabel.Match($_)
- if ($m.Success) {
- $dicLabelLocal4B[$m.Groups[1].Value] = $_
- } else {
- $dicLabelLocal4B[$_] = $_
- }
- } else {
- [void]$CLocal.Add($_)
- }
- } else {
- # stack=1
- if ($_.trim().StartsWith("┋") -or $_.trim().StartsWith("┇")) {
- $m = $reLabel.Match($_)
- if ($m.Success) {
- $dicLabelLocal4B[$m.Groups[1].Value] = $_
- } else {
- $dicLabelLocal4B[$_] = $_
- }
- } else {
- $stack = 0
- [void]$CLocal.Add($_)
- }
- }
- }
- # procossing prev section
- for ($i = 0; $i -lt $CLocal.Count; $i++) {
- $CLocal[$i] = $reContent.Replace($CLocal[$i], $sbContent)
- }
- foreach ($key in @($dicLabelLocal4B.Keys)) {
- if (-not $keysInContent.Contains($key)) {
- [void]$C.Add($dicLabelLocal4B[$key])
- $dicLabelLocal4B.Remove($key)
- } else {
- [void]$B.Add(($reLabel.Replace($dicLabelLocal4B[$key], $sbLabel) -replace ',\s*', '[]'))
- }
- }
- [void]$C.AddRange($CLocal)
- $CLocal.Clear()
- $keysInContent.Clear()
- $dicLabelLocal4B.Clear()
-
- $B
- '---'
- $C
复制代码
|