如何在两个单词之间提取多个字符串

问题描述 投票:0回答:1

我正在使用powershell来获取日志文件的内容。我试图提取两个已知单词之间的未知字符串。我需要对多行执行此操作,因此我想搜索并返回多个字符串。我看过很多例子,并尝试了不同的方法,但是它们没有用。

我已经使用reg ex将其范围缩小到我关心的日志中的行,但是我无法提取所需的文本。

$fails =  Select-String -Path 'C:\Users\user\Documents\wsyncmgr.log'  -Pattern "^(?=.*?\bError\b)(?=.*?\bSoftware\b)(?=.*?\bLicense\b)(?=.*?\bTerms\b)(?=.*?\bnot\b)(?=.*?\bdownloaded\b).*$"

此返回:

C:\Users\user\Documents\wsyncmgr.log:7340:Failed to sync update 817ad2a6-3ca7-4fa2-aa32-9b906a2d9fdc. Error: The Microsoft Software License Terms have not been completely 
downloaded and~~cannot be accepted. Source: Microsoft.UpdateServices.Internal.BaseApi.SoapExceptionProcessor.DeserializeAndThrow  $$<SMS_WSUS_SYNC_MANAGER><10-23-2019 
08:31:07.642+300><thread=5916 (0x171C)>
C:\Users\user\Documents\wsyncmgr.log:7341:Failed to sync update 87e13ecb-c669-43be-9e2a-01e567285031. Error: The Microsoft Software License Terms have not been completely 
downloaded and~~cannot be accepted. Source: Microsoft.UpdateServices.Internal.BaseApi.SoapExceptionProcessor.DeserializeAndThrow  $$<SMS_WSUS_SYNC_MANAGER><10-23-2019
08:31:07.643+300><thread=5916 (0x171C)>

等。

我只想提取更新的唯一ID,这样我就可以将它们全部放入变量中并在以后使用。

我最近的是$removeFirst = $fails -split "update "$removeLast = $removeFirst -split ". Error:"$removeLast[1]

C:\Users\user\Documents\wsyncmgr.log:7341:Failed to sync 
87e13ecb-c669-43be-9e2a-01e567285031
 The Microsoft Software License Terms have not been completely downloaded and~~cannot be accepted. Source: Microsoft.UpdateServices.Internal.BaseApi.SoapExceptionProcessor.DeserializeAndThrow  $$<SMS_WSUS_SYNC_MANAGER><10-23-2019 08:31:07.643+300><thread=5916 (0x171C)>
C:\Users\user\Documents\wsyncmgr.log:7342:Failed to sync
c1a1ec21-8efc-4cd4-8e85-90a03fc7b0c8
 The Microsoft Software License Terms have not been completely downloaded and~~cannot be accepted. Source: Microsoft.UpdateServices.Internal.BaseApi.SoapExceptionProcessor.DeserializeAndThrow  $$<SMS_WSUS_SYNC_MANAGER><10-23-2019 08:31:07.644+300><thread=5916 (0x171C)>
C:\Users\user\Documents\wsyncmgr.log:7343:Failed to sync
09dc7113-fa44-4ca8-9d70-ec254d4d2f04
 The Microsoft Software License Terms have not been completely downloaded and~~cannot be accepted. Source: Microsoft.UpdateServices.Internal.BaseApi.SoapExceptionProcessor.DeserializeAndThrow  $$<SMS_WSUS_SYNC_MANAGER><10-23-2019 08:31:07.644+300><thread=5916 (0x171C)>

但这只会删除我指定的单词,并将其余的放在单独的行上。然后数组只返回我指定的行,但我要多个。我想消除“更新”之前的所有内容以及“。错误之后”的所有内容。每行只保留“ 09dc7113-fa44-4ca8-9d70-ec254d4d2f04”。

使用正则表达式无法获得任何帮助,将不胜感激

regex string powershell find
1个回答
0
投票

在字符串上使用[regex]::matches方法(您必须提供一个正则表达式进行匹配,然后检查返回的数组以查找字符串中的每个特定匹配。这是一个示例:

$myString = "The quick brown fox"
$myMatches = [regex]::matches($myString, "\w+")
$myMatches.Value

上面的示例在字符串中查找彼此相邻的单词字符。 matches方法匹配多次,而-match运算符没有全局匹配选项(我可以发现,很想证明这里是错误的)。 .Value属性包含实际匹配项,尽管也可以在System.Text.RegularExpressions.Match对象上使用其他有用的成员。

© www.soinside.com 2019 - 2024. All rights reserved.