我有 200 个包含文本行的文件,它们可能看起来像以下任何一种模式:
{hello} "this is an example" {{ config(alias="customertable") }}
{{hello}} "this is an example" {{ config ( alias = 'monthly revenue') }}
{ { config ( alias = 'record 3 breaking news') } } {{hello}} "this is an example"
{{hello}} 'this is an example' { { config( alias = "designer bags 4 u") }} {{hello}} "this is an example"
单词alias后面的字符串始终用单引号或双引号引起来。我想将_link放在该字符串的末尾,因此它看起来像这样:“customertable_link”或“monthly revenue_link”,并想使用 powershell。我希望返回整行,因此第四个示例应该返回以下内容:
{{hello}}'这是一个例子'{ { config( alias = "designer bags 4 u_link") }} {{hello}}“这是一个例子”
例如,在一行上搜索字符串别名,然后在"或'的第二个实例左侧添加_link。
这是我目前所拥有的:
$string -replace 'alias=(?:[^"]*"){2}', '$0_link'
对于第一个例子来说,这几乎是可行的(只是引用的位置不对),但我无法正确理解。任何帮助都将不胜感激!
您可以使用
并替换为
$1_link$2
。参见正则表达式演示。
在 Powershell 中它看起来像
细节:
\b
- 单词边界(alias\s*=\s*(["'])(?:(?!\2).)*)
第 1 组:alias
- 一个词\s*=\s*
- 一个=
字符,包含在零个或多个空格内(["'])
- 第 2 组:a"
或'
(?:(?!\2).)*
- 除换行符之外的任意零个或多个字符,尽可能不等于捕获到第 2 组中的字符\2
- 要么"
被'
捕获到第 2 组