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?
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.
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.
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.
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.
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.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With