Solved

Named group regular expressions in Powershell

Posted on 2013-01-15
2
889 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell database 5 44
Powershell- Registry HIVE 6 63
Export Photos from Active Directory using Powershell 4 26
Export Active Directory Members from a Group 15 21
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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