正则表达式目标替换为命名捕获

时间:2019-03-30 11:07:12

标签: regex powershell

给出

$line = '{initError-[cf][3]}_Invalid nodes(s): [3]'

我可以使用

$line -match '^\{(?<type>[a-z]+)(-\[(?<target>(C|F|CF))\])?(\[(?<tab>\d+)\])?\}_(?<string>.*)'

并且$matches['tab']的值正确为3。但是,如果我想增加该值而又不影响字符串部分的[3],事情会变得更加复杂。我可以使用$tabIndex = $line.indexOf("[$tab]")获取第一次出现的索引,也可以使用$newLine = ([regex]"\[$tab\]").Replace($line, '[4]', 1)仅替换第一次出现的索引。但是我想知道,有没有办法更直接地做到这一点?并不是绝对必要的,因为我只想替换形式非常一致的初始{} _内的内容,因此替换一审实例有效,只是想知道我是否错过了一个更优雅的解决方案,这也可能在不同情况下需要。

2 个答案:

答案 0 :(得分:0)

我会稍微修改正则表达式,因为不建议将命名捕获与编号捕获混合使用,所以它变成这样:

'^\{(?<type>[a-z]+)(?:-\[(?<target>[CF]{1,2})\])?(?:\[(?<tab>\d+)\])?\}_(?<string>.*)'

然后您可以像下面那样使用它替换tab值:

$line        = '{initError-[cf][3]}_Invalid nodes(s): [3]'
$newTabValue = 12345

$line -replace '^\{(?<type>[a-z]+)(?:-\[(?<target>[CF]{1,2})\])?(?:\[(?<tab>\d+)\])?\}_(?<string>.*)', "{`${type}-[`${target}][$newTabValue]}_`${string}"

其结果将是:

  

{initError-[cf][12345]}_Invalid nodes(s): [3]

正则表达式详细信息:

^                    Assert position at the beginning of the string
\{                   Match the character “{” literally
(?<type>             Match the regular expression below and capture its match into backreference with name “type”
   [a-z]             Match a single character in the range between “a” and “z”
      +              Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?:                  Match the regular expression below
   -                 Match the character “-” literally
   \[                Match the character “[” literally
   (?<target>        Match the regular expression below and capture its match into backreference with name “target”
      [CF]           Match a single character present in the list “CF”
         {1,2}       Between one and 2 times, as many times as possible, giving back as needed (greedy)
   )
   \]                Match the character “]” literally
)?                   Between zero and one times, as many times as possible, giving back as needed (greedy)
(?:                  Match the regular expression below
   \[                Match the character “[” literally
   (?<tab>           Match the regular expression below and capture its match into backreference with name “tab”
      \d             Match a single digit 0..9
         +           Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   )
   \]                Match the character “]” literally
)?                   Between zero and one times, as many times as possible, giving back as needed (greedy)
\}                   Match the character “}” literally
_                    Match the character “_” literally
(?<string>           Match the regular expression below and capture its match into backreference with name “string”
   .                 Match any single character that is not a line break character
      *              Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)

答案 1 :(得分:0)

增加括号中第一个数字的另一种方法是使用(Import-Csv Job.csv) -On ContactId运算符来访问要更改的数字:

-Split

输出:

$line = '{initError-[cf][3]}_Invalid nodes(s): [3]'
$NewLine = $line -split "(\d+)"
$NewLine[1] = [int]$newLine[1] + 1
-join $NewLine