如何使用正则表达式过滤字符串的一部分?
How do I filter part of a string using regex?
使用 PowerShell 我能够从包含所需文本的文件中获取行:
Get-ChildItem -recurse -Attributes !Directory+!System | Get-Content | Select-String "tshtml"
结果如下:
templateUrl: '/tshtml/generic/gkeyvalue/gkeyvalue.html'
templateUrl: '/tshtml/generic/permission/permission.html'
templateUrl: '/tshtml/generic/permission2/permission2.html'
但现在我想要其中的一部分作为输出:
/generic/gkeyvalue/gkeyvalue.html
/generic/permission/permission.html
/generic/permission2/permission2.html
我该怎么做?我正在阅读有关正则表达式的内容,但我对此知之甚少:(
... | Select-String "tshtml" | ForEach { $_.Line -replace "^.*(?=/generic)|'$" }
替换 /generic
之前的行的开头和行尾的 '
,什么都没有。
或
Select-String "tshtml" | ForEach { if ($_.Line -match "'/tshtml(.*)'") { $Matches[1] } }
捕获''
之间的文本并输出。
试试下面的方法:
... | Select-String "tshtml"|awk '{print }'| tr -d "'"
使用 PowerShell 我能够从包含所需文本的文件中获取行:
Get-ChildItem -recurse -Attributes !Directory+!System | Get-Content | Select-String "tshtml"
结果如下:
templateUrl: '/tshtml/generic/gkeyvalue/gkeyvalue.html'
templateUrl: '/tshtml/generic/permission/permission.html'
templateUrl: '/tshtml/generic/permission2/permission2.html'
但现在我想要其中的一部分作为输出:
/generic/gkeyvalue/gkeyvalue.html
/generic/permission/permission.html
/generic/permission2/permission2.html
我该怎么做?我正在阅读有关正则表达式的内容,但我对此知之甚少:(
... | Select-String "tshtml" | ForEach { $_.Line -replace "^.*(?=/generic)|'$" }
替换 /generic
之前的行的开头和行尾的 '
,什么都没有。
或
Select-String "tshtml" | ForEach { if ($_.Line -match "'/tshtml(.*)'") { $Matches[1] } }
捕获''
之间的文本并输出。
试试下面的方法:
... | Select-String "tshtml"|awk '{print }'| tr -d "'"