【发布时间】:2019-01-15 18:18:02
【问题描述】:
我想从 .reg 文件(REG EXPORT 文件)中获取值,以便将它们与另一个 .reg 文件进行比较。我在为此创建 RegEx 时遇到问题。
让我更难的事实:
- 我不知道文件中使用了哪种注册表项类型(这就是为什么我要为所有不同类型(如字符串、dword、qword、多字符串...)构建一个正则表达式
- 不知道文件最后一个字符是不是换行符
- 我只想返回实际值,例如
fa,ad,df,fa,ad,df,fa,ad如果 regkey 是"qword"=hex(b):fa,ad,df,fa,ad,df,fa,ad
$Text = @'
[HKEY_LOCAL_MACHINE\SOFTWARE\Test]
"String"="asfasdfasasfasdfasasfasdfasasfas"
"Binary"=hex:d3,45,34,53,45,34,53,45,34,53,45,34,53,45,34,53,45,34,5b,09,89,08,\
34,09,8a,ef,02,30,40,9a,ad,fa,d0
"DWORD"=dword:fefefefe
"multistring"=hex(7):61,00,62,00,6c,00,61,00,73,00,66,00,62,00,00,00,62,00,61,\
00,6c,00,73,00,66,00,62,00,61,00,73,00,64,00,66,00,00,00,62,00,61,00,6c,00,\
73,00,64,00,66,00,61,00,64,00,6c,00,66,00,00,00,61,00,73,00,64,00,66,00,61,\
00,73,00,64,00,66,00,00,00,61,00,73,00,64,00,66,00,00,00,61,00,73,00,64,00,\
00,00,66,00,61,00,73,00,64,00,00,00,66,00,61,00,73,00,64,00,66,00,61,00,73,\
00,66,00,61,00,73,00,64,00,66,00,00,00,61,00,73,00,64,00,66,00,61,00,73,00,\
64,00,66,00,61,00,73,00,64,00,00,00,61,00,73,00,64,00,66,00,61,00,73,00,64,\
00,66,00,00,00,00,00
"qword"=hex(b):fa,ad,df,fa,ad,df,fa,ad
'@
# this one works
$key = "multistring"
$regex = ('(?ms)\"{0}\"=hex\(7\):(.+)\n' -f [RegEx]::Escape($key))
[regex]::Matches($Text, $regex) | foreach { $_.Groups[1].Value }
# this one does not work because there is no newline after the last line...
$key2 = "qword"
$regex2 = ('(?ms)\"{0}\"=hex\(b\):(.+)\n' -f [RegEx]::Escape($key2))
[regex]::Matches($Text, $regex2) | foreach { $_.Groups[1].Value }
【问题讨论】:
-
您可以将尾随换行设为可选
\n?或尝试(\n|$) -
您可以轻松地在数据末尾添加换行符。
-
如果我尝试使用 @marekful 的示例使换行符成为可选,则返回的值始终包含直到文件末尾的所有字符,这意味着也会返回所有后续键(如果不是文件中的最后一个注册表项)
标签: regex powershell registry