如何将单行中的多个正则表达式匹配捕获到 Powershell 中的 $matches 魔术变量中?

发布于 2024-09-08 01:13:39 字数 526 浏览 4 评论 0原文

假设我有字符串 "blah blah F12 blah blah F32 blah blah blah" 并且我想匹配 F12F32,如何我将两者都捕获到 Powershell 魔术变量 $matches?

如果我在 Powershell 中运行以下代码:

$string = "blah blah F12 blah blah F32 blah blah blah"
$string -match "F\d\d"

$matches 变量仅包含 F12

我也尝试过:

$string -match "(F\d\d)"

这次 $matches 有两个项目,但都是 F12

我想要 $匹配同时包含 F12F32 以便进一步处理。我似乎找不到办法做到这一点。

我们将不胜感激所有帮助。 :)

Let's say I have the string "blah blah F12 blah blah F32 blah blah blah" and I want to match the F12 and F32, how would I go about capturing both to the Powershell magic variable $matches?

If I run the following code in Powershell:

$string = "blah blah F12 blah blah F32 blah blah blah"
$string -match "F\d\d"

The $matches variable only contains F12

I also tried:

$string -match "(F\d\d)"

This time $matches had two items, but both are F12

I would like $matches to contain both F12 and F32 for further processing. I just can't seem to find a way to do it.

All help would be greatly appreciated. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不喜欢何必死缠烂打 2024-09-15 01:13:39

您可以使用 PowerShell 2.0 中的 Select-String 来执行此操作,如下所示:

Select-String F\d\d -input $string -AllMatches | Foreach {$_.matches}

不久前我有 在 MS Connect 上请求 -matchall 运算符,此建议已作为修复关闭带有此注释:

“这是通过选择字符串的 -allmatches 参数修复的。”

You can do this using Select-String in PowerShell 2.0 like so:

Select-String F\d\d -input $string -AllMatches | Foreach {$_.matches}

A while back I had asked for a -matchall operator on MS Connect and this suggestion was closed as fixed with this comment:

"This is fixed with -allmatches parameter for select-string."

晨曦÷微暖 2024-09-15 01:13:39

我建议使用此语法,因为这样可以更轻松地处理匹配数组:

$string = "blah blah F12 blah blah F32 blah blah blah" ;
$matches = ([regex]'F\d\d').Matches($string);
$matches[1].Value; # get matching value for second occurance, F32

I suggest using this syntax as makes it easier to handle your array of matches:

$string = "blah blah F12 blah blah F32 blah blah blah" ;
$matches = ([regex]'F\d\d').Matches($string);
$matches[1].Value; # get matching value for second occurance, F32
不必在意 2024-09-15 01:13:39

我看到两种处理方式不同的场景:

  1. 提取单个模式的所有匹配
  2. 提取多个模式的单个匹配

1. 提取一个模式的所有匹配: select-string + -allmatches

  • 例如正则表达式:(?<=jobs).*
  • 违反直觉,但您需要使用 Select-String 来处理这个问题,就像我从输出示例如下
$m = "Watch the deployment in realtime at: https://nomad.foo.net/ui/jobs/20e183af-8243-11eb-a2af-0a58a9feac2a
08:23
Watch the deployment in realtime at: https://nomad.foo.net/ui/jobs/20e130e9-8243-11eb-a2af-0a58a9feac2a"
$r = "(?<=jobs/).*"
$l = Select-String $r -InputObject $m -AllMatches | 
    Foreach {$_.matches.Value}
20e183af-8243-11eb-a2af-0a58a9feac2a
20e130e9-8243-11eb-a2af-0a58a9feac2a
$l[0]
>>> 20e183af-8243-11eb-a2af-0a58a9feac2a

2. 提取一个/多个模式的单个/第一个匹配:捕获组和 $Match[]

▶ $s = "Hello World from Mr Pavol"
▶ $r = "(World).*(Pavol)"
▶ $s -match $r
True
▶ $Matches
Name                           Value
----                           -----
2                              Pavol
1                              World
0                              World from Mr Pavol

I see 2 scenarios that are handled differently:

  1. extracting all matches of a single pattern
  2. extracting single match of multiple patterns

1. extract all matches of one pattern: select-string + -allmatches

  • e.g. regex: (?<=jobs).*
  • counter-intuitive but you need to use Select-String to handle this like I am to get ids of nomad jobs from the output exemplified below
$m = "Watch the deployment in realtime at: https://nomad.foo.net/ui/jobs/20e183af-8243-11eb-a2af-0a58a9feac2a
08:23
Watch the deployment in realtime at: https://nomad.foo.net/ui/jobs/20e130e9-8243-11eb-a2af-0a58a9feac2a"
$r = "(?<=jobs/).*"
$l = Select-String $r -InputObject $m -AllMatches | 
    Foreach {$_.matches.Value}
20e183af-8243-11eb-a2af-0a58a9feac2a
20e130e9-8243-11eb-a2af-0a58a9feac2a
$l[0]
>>> 20e183af-8243-11eb-a2af-0a58a9feac2a

2. extract a single/first match of one/multiple patterns: capture groups and $Match[]

▶ $s = "Hello World from Mr Pavol"
▶ $r = "(World).*(Pavol)"
▶ $s -match $r
True
▶ $Matches
Name                           Value
----                           -----
2                              Pavol
1                              World
0                              World from Mr Pavol
吃→可爱长大的 2024-09-15 01:13:39
$String = @'
MemberProgram PackageID="12345678" ProgramName="Install"/
MemberProgram PackageID="87654321" ProgramName="Install"/
MemberProgram PackageID="21436587" ProgramName="Install"/
MemberProgram PackageID="78563412" ProgramName="Install"/
'@
([regex]'(?<=PackageID=\")\d+(?=\")').Matches($String).value
$String = @'
MemberProgram PackageID="12345678" ProgramName="Install"/
MemberProgram PackageID="87654321" ProgramName="Install"/
MemberProgram PackageID="21436587" ProgramName="Install"/
MemberProgram PackageID="78563412" ProgramName="Install"/
'@
([regex]'(?<=PackageID=\")\d+(?=\")').Matches($String).value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文