Solved

Named group regular expressions in Powershell

Posted on 2013-01-15
2
798 Views
Last Modified: 2013-01-31
In powershell I am trying to search a file specified by the $mypath variable for a given regular expression pattern $mypattern. The regular expression uses named groups of the form '(?<age>[0-9]*Years)'. How can I get the named group value into a variable that I can use. I have tried the following as a guess but havent got anywhere.

$result = select-string -path $mypath -pattern $mypattern | foreach-object {$_.matches} | foreach-object {$_.groups['age']} | select-object value

Open in new window

0
Comment
Question by:Blowfelt82
2 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38778761
try:

select-string -path $mypath -pattern $mypattern | select -expand Matches | foreach {$_.groups["age"].value}

Open in new window

0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 38778832
Select-String seems to ignore named captures. The groups are always named like the captured value, which makes it less useful. You will have to use the numeric index into the Groups collection to get access:
select-string -path $mypath -pattern $mypattern |
  select -Expand Matches |
  foreach-object {$_.Groups[1].Value}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now