在字符串中间使用通配符的正则表达式
regex with wildcards in the middle of a string
我有大量来自 [HKEY_CURRENT_USER\Printers\Connections] 的不同用户的注册表导出。
此文件包含多行带有“
[HKEY_CURRENT_USER\Printers\Connections\,,BBPRINTER01.domain.local,AG-printer-S4]
我需要 BBPRINTER01.domain.local,AG-printer-S4
部分。问题是,BBPRINTER01
和 AG-printer-S4
可能不同。
所以我需要一个正则表达式,它从 [HKEY_CURRENT_USER\Printers\Connections\,,BBPRINTER01.domain.local,AG-printer-S4]
.
中提取“.domain.local,”
而且我没有任何线索来生成这个:(
with (?<=\[)(.*)(?=\])
我可以获得包括 []
在内的所有内容,但我不知道如何从上面只获取通配符字符串。
您可以使用
将子字符串捕获到第 1 组中
\[[^][]*?(\w+\.domain\.local,[^][]*)]
参见 this regex demo。匹配
\[
- 一个 [
字符
[^][]*?
- [
和 ]
以外的 0+ 个字符尽可能少
(
- 捕获组 1 的开始
\w+
- 一个或多个 letters/digits/_
\.domain\.local,
- .domain.local,
字符串
[^\]\[]*
- ]
和 [
以外的 0 个或更多字符
)
捕获组结束
]
- 一个 ]
字符。
在 PowerShell 中,使用
$result = Get-Content $file | Select-String -Pattern '\w+\.domain\.local,[^][]*'
if ($result.Matches.Success) { $result.Matches.Groups[1].Value }
# => BBPRINTER01.domain.local,AG-printer-S4
PowerShell 测试:
如果你需要从一个文件夹中的多个文件中解析这个,你可以使用
$files = Get-ChildItem -Path 'Path\To\The\Files' -File
foreach ($file in $files) {
Get-Content -Path $file.FullName |
Select-String -Pattern '\[.*,([^,]+,[^\]]+)\]' -AllMatches |
ForEach-Object { $_.Matches.Groups[1].Value }
}
正则表达式详细信息
\[ Match the character “[” literally
. Match any single character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
, Match the character “,” literally
( Match the regex below and capture its match into backreference number 1
[^,] Match any character that is NOT a “,”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
, Match the character “,” literally
[^\]] Match any character that is NOT a “]”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\] Match the character “]” literally
结果可能类似于
BBPRINTER01.domain.local,AG-printer-S4
BBPRINTER02.otherdomain.local,AG-printer-S5
BBPRINTER03.somedomain.local,AG-printer-S6
编辑
根据您的评论,要同时输出文件路径和正则表达式匹配项,最简洁的方法是输出 objects 而不是字符串,并将这些结果捕获到变量中。使用对象,您还有机会写入可在 Excel:
中打开的结构化 CSV 文件
$files = Get-ChildItem -Path 'Path\To\The\Files' -File
$result = foreach ($file in $files) {
Get-Content -Path $file.FullName |
Select-String -Pattern '\[.*,([^,]+,[^\]]+)\]' -AllMatches |
ForEach-Object {
[PsCustomObject]@{
'SourceFile' = $file.FullName
'Regex-Output' = $_.Matches.Groups[1].Value
}
}
}
# output on screen
$result | Format-Table -AutoSize
# write to CSV file
$result | Export-Csv -Path 'Path\To\The\result.csv' -UseCulture -NoTypeInformation
我有大量来自 [HKEY_CURRENT_USER\Printers\Connections] 的不同用户的注册表导出。 此文件包含多行带有“
[HKEY_CURRENT_USER\Printers\Connections\,,BBPRINTER01.domain.local,AG-printer-S4]
我需要 BBPRINTER01.domain.local,AG-printer-S4
部分。问题是,BBPRINTER01
和 AG-printer-S4
可能不同。
所以我需要一个正则表达式,它从 [HKEY_CURRENT_USER\Printers\Connections\,,BBPRINTER01.domain.local,AG-printer-S4]
.
而且我没有任何线索来生成这个:(
with (?<=\[)(.*)(?=\])
我可以获得包括 []
在内的所有内容,但我不知道如何从上面只获取通配符字符串。
您可以使用
将子字符串捕获到第 1 组中\[[^][]*?(\w+\.domain\.local,[^][]*)]
参见 this regex demo。匹配
\[
- 一个[
字符[^][]*?
-[
和]
以外的 0+ 个字符尽可能少(
- 捕获组 1 的开始\w+
- 一个或多个 letters/digits/_
\.domain\.local,
-.domain.local,
字符串[^\]\[]*
-]
和[
以外的 0 个或更多字符
)
捕获组结束]
- 一个]
字符。
在 PowerShell 中,使用
$result = Get-Content $file | Select-String -Pattern '\w+\.domain\.local,[^][]*'
if ($result.Matches.Success) { $result.Matches.Groups[1].Value }
# => BBPRINTER01.domain.local,AG-printer-S4
PowerShell 测试:
如果你需要从一个文件夹中的多个文件中解析这个,你可以使用
$files = Get-ChildItem -Path 'Path\To\The\Files' -File
foreach ($file in $files) {
Get-Content -Path $file.FullName |
Select-String -Pattern '\[.*,([^,]+,[^\]]+)\]' -AllMatches |
ForEach-Object { $_.Matches.Groups[1].Value }
}
正则表达式详细信息
\[ Match the character “[” literally
. Match any single character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
, Match the character “,” literally
( Match the regex below and capture its match into backreference number 1
[^,] Match any character that is NOT a “,”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
, Match the character “,” literally
[^\]] Match any character that is NOT a “]”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\] Match the character “]” literally
结果可能类似于
BBPRINTER01.domain.local,AG-printer-S4
BBPRINTER02.otherdomain.local,AG-printer-S5
BBPRINTER03.somedomain.local,AG-printer-S6
编辑
根据您的评论,要同时输出文件路径和正则表达式匹配项,最简洁的方法是输出 objects 而不是字符串,并将这些结果捕获到变量中。使用对象,您还有机会写入可在 Excel:
中打开的结构化 CSV 文件$files = Get-ChildItem -Path 'Path\To\The\Files' -File
$result = foreach ($file in $files) {
Get-Content -Path $file.FullName |
Select-String -Pattern '\[.*,([^,]+,[^\]]+)\]' -AllMatches |
ForEach-Object {
[PsCustomObject]@{
'SourceFile' = $file.FullName
'Regex-Output' = $_.Matches.Groups[1].Value
}
}
}
# output on screen
$result | Format-Table -AutoSize
# write to CSV file
$result | Export-Csv -Path 'Path\To\The\result.csv' -UseCulture -NoTypeInformation