发布
社区首页 >问答首页 >使用正则表达式提取匹配项,然后将它们传递到一个数组中

使用正则表达式提取匹配项,然后将它们传递到一个数组中
EN

Stack Overflow用户
提问于 2013-06-28 05:14:41
回答 1查看 13.6K关注 0票数 6
代码语言:javascript
代码运行次数:0
复制
 $htmltitle1 = "{Quit|Discontinue|Stop|Cease|Give Up} Tottenham Manager {HEY}"
  $reg = "\{.*?\}"
  $found = $htmltitle1 -match $reg

  $spuntext = @()
  If ($found)
    {    
         ([regex]$reg).matches($htmltitle1)  
    }

我可以看到$matches (如下所示),但是如何将每个匹配项提取到$spuntext数组中?哈哈,我已经花了几个小时尝试不同的东西了。

代码语言:javascript
代码运行次数:0
复制
Groups   : {{Quit|Discontinue|Stop|Cease|Give Up}}
Success  : True
Captures : {{Quit|Discontinue|Stop|Cease|Give Up}}
Index    : 0
Length   : 37
Value    : {Quit|Discontinue|Stop|Cease|Give Up}

Groups   : {{HEY}}
Success  : True
Captures : {{HEY}}
Index    : 56
Length   : 5
Value    : {HEY}

Key   : 0
Value : {Quit|Discontinue|Stop|Cease|Give Up}
Name  : 0
EN

回答 1

Stack Overflow用户

发布于 2013-06-29 04:46:21

我今天在尝试学习语法后也想出了这个,以防它对任何像我一样困惑的新手有帮助:(在v2中工作)

代码语言:javascript
代码运行次数:0
复制
$htmltitle1 = "{Quit|Discontinue|Stop|Cease|Give Up} Tottenham Manager {HEY}"
$reg = "{.*?}"
$found = $htmltitle1 -match $reg
$spuntext = @()

If ($found)
  {    
      [regex]::matches($htmltitle1, $reg) | % {$spuntext += $_.Value}

  }



$spuntext 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17353326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档