Powershell正则表达式捕获新行后的字符串



我用这个正则表达式捕获的时间最长。基本上,我导入了一个csv,脚本读取列中的文本,并捕获文本中的字符串,将其放入变量中。我遇到的问题是,捕获也捕获了回车。我怎样才能不捕捉回车?

例如,文本显示:

check the following key:
HKLM:Softwareyaddayadda

代码:

if($file.col2 -match "following key:(?<path>s*(S+s*S+))"){
$Matches['path']
}

如果您的两行字符串通过类似Get-Content -Path $FullFileName -Raw的方式放置在$InStuff中,则可以使用(?m)多行regex选项和命名捕获组将路径放入$Matches.Path中,如下所示。。。

$InStuff = @'
check the following key:
HKLM:Softwareyaddayadda
'@
$Null = $InStuff -match '(?m)(?<Path>.+:\.+)$'
$Path = $Matches.Path
$Path

输出=HKLM:Softwareyaddayadda

最新更新