Powershell获取引号之间的内容

标签 powershell

我得到这样的输出

Fila 28:
  Nombre del solicitante: "CONTOSO\X000000"

Fila 29:
  Nombre del solicitante: "CONTOSO\E000000"

Fila 30:
  Nombre del solicitante: "CONTOSO\X111111"

我需要过滤除引号内的内容之外的所有内容

最佳答案

另一个可能更直观的解决方案,使用正则表达式和 -match 和 -replace

$text = 'Nombre del solicitante: "CONTOSO\X111111"'

$regex = '\s*Nombre del solicitante: "([^"]+)"\s*'
@($text) -match $regex -replace $regex,'$1'

作为一句台词:

@(<command>) -match ''Nombre del solicitante:' -replace '.*"(.+)".*','$1'

将命令输出包装在 @() 中是为了确保返回一个数组,即使该命令只返回一行,以便 -match 运算符充当过滤器而不是 bool true/false 测试。

@JNK - 您是否使用数组进行测试(这就是命令输出将产生的结果)?

$text = 
(@'
Fila 28:
  Nombre del solicitante: "CONTOSO\X000000"

Fila 29:
  Nombre del solicitante: "CONTOSO\E000000"

Fila 30:
  Nombre del solicitante: "CONTOSO\X111111"
'@).split("`n") 

$regex = '\s*Nombre del solicitante: "([^"]+)"\s*'
@($text) -match $regex -replace $regex,'$1'


CONTOSO\X000000
CONTOSO\E000000
CONTOSO\X111111

编辑 - 仅获取帐户名,不包含域部分:

$regex = '\s*Nombre del solicitante: ".+?\\([^"]+)"\s*'

关于Powershell获取引号之间的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22299660/

相关文章:

使用 send-mailmessage 命令时的电子邮件凭据

powershell - 以编程方式独立于构建触发发布

Powershell - 对多个文件的多个命令

powershell - Server 2012 R2 缺少 PSReadline cmdlet?

Powershell:从委托(delegate)引用局部变量

regex - Azure-CLI/Powershell 密码要求

json - Azure 函数和 Powershell : get response in plain JSON

使用 $matches 数组中的值时出现 PowerShell Set-Item 奇怪现象

powershell - 如何在PowerShell中循环将某些文件与字符串匹配?

powershell - 如何在 powershell 中设置默认参数值?