Powershell忽略了正则表达式后面的内容以返回整行

use*_*742 7 regex powershell

我希望这是一个简单的问题.

我有一个文本日志文件,其中包含以下行:

123,010502500114082000000009260000000122001T

我想搜索日志文件并返回上面文本的"00000000926"部分.所以我写了一个正则表达式:(?<= 123. {17}).{11}

因此,当文本后面的内容等于'123'时有17个字符,返回下一个11.这在在线正则表达式编辑器上测试时工作正常.但是在Powershell中,返回整行而不是我想要的11个字符,我无法理解为什么.

$InputFile = get-content logfile.log
$regex = '(?<=123.{17}).{11}'
$Inputfile | select-string $regex
Run Code Online (Sandbox Code Playgroud)

(返回整行).

为什么powershell会返回整条线路?

Mat*_*att 6

不要打折Select-String.就像Briantist说它正在做你想要的,你需要以两种方式之一提取你真正想要的数据.Select-String返回Microsoft.PowerShell.Commands.MatchInfo对象而不仅仅是原始字符串.此外,我们将使用Select-String直接获取文件输入的能力.

$InputFile = "logfile.log"
$regex = '(?<=123.{17}).{11}'
Select-string $InputFile -Pattern $regex | Select-Object -ExpandProperty  Matches | Select-Object -ExpandProperty Value 
Run Code Online (Sandbox Code Playgroud)

如果您至少拥有PowerShell 3.0

(Select-string $InputFile -Pattern $regex).Matches.Value
Run Code Online (Sandbox Code Playgroud)

这两种情况都给出了

00000009260
Run Code Online (Sandbox Code Playgroud)


bri*_*ist 5

这是因为你正在使用Select-String它返回匹配的行(想想grep).

$InputFile = get-content logfile.log | ForEach-Object {
    if ($_ -match '(?<=123.{17})(.{11})') {
        $Matches[1]
    }
}
Run Code Online (Sandbox Code Playgroud)

没有测试过,但它应该工作(或类似的东西).


mjo*_*nor 5

你真的不需要外观正则表达式:

$InputFile = get-content logfile.log
$InputFile -match '123.{28}' -replace '123.{17}(.{11}).+','$1'
Run Code Online (Sandbox Code Playgroud)