regex - PowerShell RegEx:从字符串获取SID

标签 regex powershell

我正在尝试运行powershell命令以从字符串返回SID。

$string = "The SID is S-1-9-1551374245-3853148685-361627209-2951783729-4075502353-0-0-0-0-3"

Select-String -Pattern "\bS-\d{1}-\d{1}-\d{10}-\d{10}-\d{9}-\d{10}-\d{10}-\d{1}-\d{1}-\d{1}-\d{1}-\d{1}\b" -InputObject $string 

当我运行上面的代码时,它返回整个字符串,但是我只想要SID#,即S-1-9-1551374245-3853148685-361627209-2951783729-4075502353-0-0-0-0-3'

最佳答案

$Pattern = 'S-\d-(?:\d+-){1,14}\d+'
$Matches =  Select-String -Pattern $Pattern -InputObject $string
if ( $Matches ) { $Matches.Matches.Value }

归功于vs97's regex pattern

关于regex - PowerShell RegEx:从字符串获取SID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58036023/

相关文章:

java - 与 Java 中单词末尾的子字符串之一匹配的正确正则表达式是什么?

string - 如何使用多个字符的字符串在 PowerShell 中 "split"字符串?

git - 使用带有 powershell 的 git 和带有密码的 ssh key

powershell - 从 CSV 获取时出现错误 "Get-ADComputer : Cannot find an object with identity"

java - 将数字提取到字符串数组中

python - 使用Python替换文本文件中除文件名之外的所有文件路径

c# - 使用正则表达式忽略特定字符串

使用正则表达式 c 检查 url

git - 使用多个带有 SSH key 的 Github 帐户

powershell - 使用 PowerShell 删除本地 Windows 配置文件