Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting multiple matches from a string with PowerShell

Posted on 2014-04-03
4
Medium Priority
?
2,662 Views
Last Modified: 2014-04-03
I have a bunch of log files that I'd like to parse through and pull string data out of, but the RegEx on this one is getting a little outside of my level of expertise.  I'm basically going to use code similar to that found here :
$input_path = 'c:\test\file.txt'
$regex = '' #Need help with this part
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }

Open in new window


What I really need help with is figuring out the RegEx for these log files.  Here is an example of what they look like:

<option value="954393">LASTNAME1, FIRSTNAME1 - &#x5b;lfirstname1&#x40;domain.com&#x5d;</option>

<option value="819613">LASTNAME2, FIRSTNAME2 - &#x5b;firstname2.lastname2&#x40;domain.com&#x5d;</option>

<option value="1151900">LASTNAME3, FIRSTNAME3 - &#x5b;firstname3.lastname3&#x40;domain.com&#x5d;</option>

Open in new window


What I'm trying to match and pull out is the number inside the value="", the LASTNAME, FIRSTNAME and then I want the string between &#x5b; and &#x40; (which may or may not contain a single period in it).

Are there any RegEx experts out there that can assist?
0
Comment
Question by:Souladin
[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
  • 3
4 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39976136
Try this:
$regex = "<option value=""(\d*)"">([\w ]*), ([\w ]*)- &#x5b;(.*)&#x40;"

Open in new window

You'll find the number in $1, the lastname in $2, the firstname in $3 and the last string in $4.

HTH,
Dan
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 39976178
BTW, if you want to see the values, you need to do it like this:
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches.Groups } | % { $_.Value }

Open in new window

It will output something like this:
<option value="954393">LASTNAME1, FIRSTNAME1 - &#x5b;lfirstname1&#x40;
954393
LASTNAME1
FIRSTNAME1 
lfirstname1
<option value="819613">LASTNAME2, FIRSTNAME2 - &#x5b;firstname2.lastname2&#x40;
819613
LASTNAME2
FIRSTNAME2 
firstname2.lastname2
<option value="1151900">LASTNAME3, FIRSTNAME3 - &#x5b;firstname3.lastname3&#x40;
1151900
LASTNAME3
FIRSTNAME3 
firstname3.lastname3

Open in new window

Without groups you would only see the original strings.
0
 

Author Closing Comment

by:Souladin
ID: 39976302
That was way less complicated than I thought it would be.  Looks like I need to take that regex refresher class again.  Thanks!
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39976311
Glad I could help!

You could read some answers and then answer a few questions here. Bet it would trump that introductory course :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

609 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