我必须从这一行中提取用户信息
"user/data ^`ms\john ^`Lorem Ipsum Lorem ^`Lorem Ipsum Lorem"
模式是信息始终位于"user/data ^
“和"^
”
预期结果是“ms \ john”
这是我的尝试,
$line = "user/data ^`ms\john ^`Lorem Ipsum Lorem ^`Lorem Ipsum Lorem"
if ($line -match "user/data(.*)")
{
write-host "found: $($matches[1])"
} else {
Write-Host "no found"
}
我不知道如何在正则表达式中添加特殊字符并提取“ms \ john”。
欢迎任何评论。
答案 0 :(得分:2)
你也可以试试这个:
$line = "user/data ^`ms\john ^`Lorem Ipsum Lorem ^`Lorem Ipsum Lorem"
if ($line -match "user/data\s+\^`?([^\^]+)")
{
write-host "found: $($matches[1])"
} else {
Write-Host "no found"
}
答案 1 :(得分:1)
试试这个:
$line = "user/data ^`ms\john ^`Lorem Ipsum Lorem ^`Lorem Ipsum Lorem"
if ($line -match "(?<=\^)(.[^\^]*)")
{
write-host "found: $($matches[1])"
} else {
Write-Host "no found"
}
答案 2 :(得分:1)
还有两个选择:
PS> $il = "user/data ^`ms\john ^`Lorem Ipsum Lorem ^`Lorem Ipsum Lorem"
PS> $il -replace '^user/data\ \^`([^\^]+)\ \^.+$','$1'
ms\john
PS> [regex]::Matches($il,'user/data\s\^`([^\^]+)\s\^').Groups[1].Value
ms\john