Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you extract the value of a regex backreference/match in Powershell

Tags:

I have a text file containing lines of data. I can use the following powershell script to extract the lines I'm interested in:

select-string -path *.txt -pattern "subject=([A-Z\.]+)," 

Some example data would be:

blah blah subject=THIS.IS.TEST.DATA, blah blah blah 

What I want is to be able to extract just the actual contents of the subject (i.e. the "THIS.IS.TEST.DATA" string). I tried this:

select-string -path *.txt -pattern "subject=([A-Z\.]+)," | %{ $_.Matches[0] } 

But the "Matches" property is always null. What am I doing wrong?

like image 919
d4nt Avatar asked Mar 05 '09 12:03

d4nt


People also ask

How do you use regex expressions in PowerShell?

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data. Matches the beginning of the line. Matches the end of the line.

Can you do regex in PowerShell?

One of the most useful and popular PowerShell regex operators is the match and notmatch operators. These operators allow you to test whether or not a string contains a specific regex pattern. If the string does match the pattern, the match operator will return a True value.

How do I use match command in PowerShell?

Definition of PowerShell Match. PowerShell match operators (Like, NotLike, Match, NotMatch) checks if the Input string or keyword matches the specific keyword using the provided pattern or the Wildcard. Patterns and the Wildcard used depends on the operator that is used.

What regex flavor does PowerShell use?

The correct flavor is . Net regex, but in online testers like debuggex that don't have it, I use PCRE and it seems to work fairly well.


2 Answers

I don't know why your version doesn't work. It should work. Here is an uglier version that works.

$p = "subject=([A-Z\.]+)," select-string -path *.txt -pattern $p | % {$_ -match $p > $null; $matches[1]} 

Explanation:

-match is a regular expression matching operator:

>"foobar" -match "oo.ar" True 

The > $null just suppresses the True being written to the output. (Try removing it.) There is a cmdlet that does the same thing whose name I don't recall at the moment.

$matches is a magic variable that holds the result of the last -match operation.

like image 171
dan-gph Avatar answered Oct 03 '22 22:10

dan-gph


In PowerShell V2 CTP3, the Matches property is implemented. So the following will work:

select-string -path *.txt -pattern "subject=([A-Z\.]+)," | %{ $_.Matches[0].Groups[1].Value } 
like image 26
Philippe Avatar answered Oct 03 '22 22:10

Philippe