regex - RegEx 目标替换为命名捕获
问题描述
给定
$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)
用来只替换第一次出现。但我想知道,有没有办法更直接地解决这个问题?这不是绝对必要的,因为我只想替换初始 {}_ 中的东西,它具有非常一致的形式,所以替换第一个实例是可行的,只是想知道我是否错过了一个更优雅的解决方案,这也可能在不同的情况下需要。
解决方案
我会稍微更改一下正则表达式,因为不建议将命名捕获与编号捕获混合,所以它变成了这样:
'^\{(?<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)
)
推荐阅读
- sql-server - 使用存储过程更改表中的多个列
- google-cloud-platform - 从谷歌云中的虚拟机实例更改实例模板
- android - 在 Flutter 中设置图像壁纸意图
- pytorch - pytorch 4d numpy 数组在自定义数据集中应用转换
- c# - 在 Unity (C#) 中,为什么会出现 NullReferenceException 以及如何修复它?
- javascript - 如何在 FormControl 中使用 mat-slide-toggle?
- linux - 是否可以在 mime 定义文件的 glob 模式中使用 unicode 字符?
- powershell - Powershell:用 ForEach-Object 替换的顺序
- c# - WPF 为每个视图创建一个按钮,将其设置为 ContentControl 的内容
- reactjs - 登录成功时如何在redux saga中路由跳转?