使用Powershell匹配提取值的文本

时间:2016-04-17 15:01:43

标签: regex powershell pattern-matching

对于本文:

 https://twitter.com/Guardiola31337/status/719108651781332994 …
<li >< a target="_blank" href =" https://twitter.com/gdgbarcelona ">< i class="fa fa-twitter" >< / i><  /a>< /li>
<li >< a target="_blank" href=" https://twitter.com/gdgcaceres "><i class="fa fa-twitter">< / i >< /a ></ li >
< li >< a target="_blank" href =" https://twitter.com/decharlas " >< i class="fa fa-twitter">< / i></a ></ li >
<li> < a target="_blank" href = " https://twitter.com/gdggranada " > < i class="fa fa-twitter" >< / i>< /a >< /li >

如何提取https://twitter.com/ {值}?

我试试这个,但是我收到了错误:

$text | Select-String "(https://twitter.com/)((?:[A-Za-z0-9-_]*))" -AllMatches |
        Foreach {$_.Matches} | Foreach { $usernames2 += "-" + $_.Value}


$text -match '(https://twitter.com/)((?:[A-Za-z0-9-_]*))'

1 个答案:

答案 0 :(得分:0)

这个正则表达式适合你:https:\/\/twitter\.com\/([^"]+) Demo here

示例:

$fetchTwitterPath = 'https:\/\/twitter\.com\/([^"]+)'

[regex]::Matches($text, $fetchTwitterPath) | foreach { $_.Groups.Value[1].Trim() }

输出:

Guardiola31337/status/719108651781332994
gdgbarcelona
gdgcaceres
decharlas
gdggranada

注意:我在示例的第一行添加了",因为它已丢失。