0

我必须从这一行中提取用户信息

"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”。

欢迎任何意见。

4

3 回答 3

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"
}
于 2012-12-23T22:20:27.243 回答
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
于 2012-12-24T07:12:28.420 回答
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"
}
于 2012-12-23T20:56:40.260 回答