How to get the captured groups from Select-String?

前端 未结 5 803
清酒与你
清酒与你 2020-12-05 16:41

I\'m trying to extract text from a set of files on Windows using the Powershell (version 4):

PS > Select-String -AllMatches -Pattern 

        
5条回答
  •  鱼传尺愫
    2020-12-05 17:29

    Have a look at the following

    $a = "http://192.168.3.114:8080/compierews/" | Select-String -Pattern '^http://(.*):8080/(.*)/$' 
    

    $a is now a MatchInfo ($a.gettype()) it contain a Matches property.

    PS ps:\> $a.Matches
    Groups   : {http://192.168.3.114:8080/compierews/, 192.168.3.114, compierews}
    Success  : True
    Captures : {http://192.168.3.114:8080/compierews/}
    Index    : 0
    Length   : 37
    Value    : http://192.168.3.114:8080/compierews/
    

    in the groups member you'll find what you are looking for so you can write :

    "http://192.168.3.114:8080/compierews/" | Select-String -Pattern '^http://(.*):8080/(.*)/$'  | % {"IP is $($_.matches.groups[1]) and path is $($_.matches.groups[2])"}
    
    IP is 192.168.3.114 and path is compierews
    

提交回复
热议问题