将字符串标记为 class 的正则表达式
Regex to tokenize string to a class
msiinv 2015 14:58:10
SSMSBoost for SSMS 2012
Product code: {94EDFFE7-E4F4-4C9B-A57E-C7267BB4A777}
Product state: (5) Installed.
Assignment: per user
Package code: {5D9DA43D-E03A-4420-A8AF-3D2FFBA1A696}
Version: 2.15.5473.18051
Publisher: Solutions Crew
Language: 1033
Installed from: C:\Users\EffiaSoft\Downloads\
Package: SSMSBoostInstaller2012_2.15.5473.18051.msi
Product Icon: %APPDATA%\Microsoft\Installer\{94EDFFE7-E4F4-4C9B-A57E-C7267BB4A777}\icon.ico
Instance type: 0
Local package: C:\Windows\Installerb9554a.msi
Install date: 2015
0 patch packages.
Microsoft Application Error Reporting
Product code: {95120000-00B9-0409-0000-0000000FF1CE}
Product state: (5) Installed.
Assignment: per machine
Package code: {420F351B-33A5-4A58-A856-69B2EDEDC8F7}
Version: 12.0.6012.5000
Publisher: Microsoft Corporation
Language: 1033
Installed from: c:\f04684676d077419cb\redist\watson\
Package: dw20shared.msi
About link: http://support.microsoft.com
Help link: http://support.microsoft.com
Instance type: 0
Local package: c:\Windows\Installer3d6.msi
Install date: 2014
0 patch packages.
我正在尝试标记此文本。我期望的结果是 class 调用 Software
,它将具有 ProductCode
、ProductState
等属性以及文本中定义的所有其他属性,并使用冒号。所以解析这个文件会给我 Software
class 的列表。你认为我应该如何处理这件事。
由于我的代表(这有点愚蠢),我无法发表评论,但这是我的建议。
它可能不是一个干净的解决方案,但如果这是你唯一的输出,那么你总是可以通过换行符拆分字符串,然后循环它给你的数组并使用以下正则表达式来获取第一个分号前的值
^\D+(?=:\s)
然后您需要某种开关来确定您需要将软件 class 的 属性 放入其中。它可能很乱,但看起来好像输出的文本可以很安全地假设它在很大程度上是相同的。
由于有可变数量的空格选项卡和不同的字符来获取 属性 的值,我只需使用上面的正则表达式将 属性 名称替换为空,然后该数组的其余部分将是你的价值。这将减少您需要使用的正则表达式的数量,这通常是一件更好的事情。
明智的伪代码是
Split string up by new line characters
loop through collection of strings
Run the regex ^\D+(?=:\s)
switch on the regex string to find the property name
replace property name with blank space using the regex ^\D+:\s+
using the rest of the string as the value set the property.
我不知道你为什么被标记下来,我怀疑是因为你提到了正则表达式。如果可能,更好的解决方案(并且可能不会被标记下来)是将文本文件转换为 xml 文件。我不知道你的输出有多大可能,但让它成为更好的解决方案。
编辑:更新了正则表达式以处理评论中的例外情况。
msiinv 2015 14:58:10
SSMSBoost for SSMS 2012
Product code: {94EDFFE7-E4F4-4C9B-A57E-C7267BB4A777}
Product state: (5) Installed.
Assignment: per user
Package code: {5D9DA43D-E03A-4420-A8AF-3D2FFBA1A696}
Version: 2.15.5473.18051
Publisher: Solutions Crew
Language: 1033
Installed from: C:\Users\EffiaSoft\Downloads\
Package: SSMSBoostInstaller2012_2.15.5473.18051.msi
Product Icon: %APPDATA%\Microsoft\Installer\{94EDFFE7-E4F4-4C9B-A57E-C7267BB4A777}\icon.ico
Instance type: 0
Local package: C:\Windows\Installerb9554a.msi
Install date: 2015
0 patch packages.
Microsoft Application Error Reporting
Product code: {95120000-00B9-0409-0000-0000000FF1CE}
Product state: (5) Installed.
Assignment: per machine
Package code: {420F351B-33A5-4A58-A856-69B2EDEDC8F7}
Version: 12.0.6012.5000
Publisher: Microsoft Corporation
Language: 1033
Installed from: c:\f04684676d077419cb\redist\watson\
Package: dw20shared.msi
About link: http://support.microsoft.com
Help link: http://support.microsoft.com
Instance type: 0
Local package: c:\Windows\Installer3d6.msi
Install date: 2014
0 patch packages.
我正在尝试标记此文本。我期望的结果是 class 调用 Software
,它将具有 ProductCode
、ProductState
等属性以及文本中定义的所有其他属性,并使用冒号。所以解析这个文件会给我 Software
class 的列表。你认为我应该如何处理这件事。
由于我的代表(这有点愚蠢),我无法发表评论,但这是我的建议。
它可能不是一个干净的解决方案,但如果这是你唯一的输出,那么你总是可以通过换行符拆分字符串,然后循环它给你的数组并使用以下正则表达式来获取第一个分号前的值
^\D+(?=:\s)
然后您需要某种开关来确定您需要将软件 class 的 属性 放入其中。它可能很乱,但看起来好像输出的文本可以很安全地假设它在很大程度上是相同的。
由于有可变数量的空格选项卡和不同的字符来获取 属性 的值,我只需使用上面的正则表达式将 属性 名称替换为空,然后该数组的其余部分将是你的价值。这将减少您需要使用的正则表达式的数量,这通常是一件更好的事情。
明智的伪代码是
Split string up by new line characters
loop through collection of strings
Run the regex ^\D+(?=:\s)
switch on the regex string to find the property name
replace property name with blank space using the regex ^\D+:\s+
using the rest of the string as the value set the property.
我不知道你为什么被标记下来,我怀疑是因为你提到了正则表达式。如果可能,更好的解决方案(并且可能不会被标记下来)是将文本文件转换为 xml 文件。我不知道你的输出有多大可能,但让它成为更好的解决方案。
编辑:更新了正则表达式以处理评论中的例外情况。