[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

regex matching help in Powershell scripting

Posted on 2011-10-10
5
Medium Priority
?
536 Views
Last Modified: 2012-05-12
Wether $user = Samson, Tom or $user = Samson, V Tom, I still get "ya'll got a middle name"
What is wrong with my regex for my third match attempt?


$user = "Employee Name: Samson, V Tom"
$user -match "Employee Name: (\w+)"
echo $matches[1]
$1 = $matches[1]
echo $1
$user -match "Employee Name: \w+, (\w+)"
echo $matches[1]
$2 = $matches[1]
echo $2
$user -match "Employee Name: \w+, \w+ (\w+)"
echo $matches[1]
$3 = $matches[1]
echo $3
if ($3) { echo "ya'll got a middle name"} else { echo "ya'll ain't got no middle name" }

Open in new window

0
Comment
Question by:_-W-_
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 36943949
The parens group the last match, which is the last name. So "Samson, V Tom" gives you "Tom". If you move the parens over to the middle match, you'll get "V" if there is a middle name.

If you remove the middle name from the user, $user -match ... will return False, which you need to check for, instead of assuming the Matches collection holds the answer. If you run the above, change the name and re-run it, the matches collection will still hold the previous results.
0
 
LVL 3

Author Comment

by:_-W-_
ID: 36944032
Hmm. This still did not work.. Am I checking $3 the wrong way? See code:
$1 = "hello"
$2 = "hello"
$3 = "hello"
$user = "hello"

$user = "Employee Name: Samson, Tom"
$user -match "Employee Name: (\w+)"
echo $matches[1]
$1 = $matches[1]
echo $1
$user -match "Employee Name: \w+, (\w+)"
echo $matches[1]
$2 = $matches[1]
echo $2
$user -match "Employee Name: \w+, (\w+) \w+"
echo $matches[1]
$3 = $matches[1]
echo $3
if ($3) { echo "ya'll got a middle name"} else { echo "ya'll ain't got no middle name" }

Open in new window

0
 
LVL 16

Accepted Solution

by:
ToddBeaulieu earned 2000 total points
ID: 36944091
You're still not checking the result of -match operation. If it's false, it doesn't make sense to look at the matches collection.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36944108
When $user equals "Employee Name: Samson, Tom", your 3rd -match call returns false, and so matches[1] isn't updated. That group refers to the match of the 2nd -match call. Test the return of your 3rd -match call before trying to use the object:

$user = "Employee Name: Samson, Tom"
$user -match "Employee Name: (\w+)"
echo $matches[1]
$1 = $matches[1]
echo $1
$user -match "Employee Name: \w+, (\w+)"
echo $matches[1]
$2 = $matches[1]
echo $2
if ($user -match "Employee Name: \w+, \w+ (\w+)")
{
  echo $matches[1]
  $3 = $matches[1]
  echo $3
}

if ($3) { echo "ya'll got a middle name"} else { echo "ya'll ain't got no middle name" }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36944124
@ToddBeaulieu

My apologies...  I didn't read your first comment carefully. I see you already mentioned what I restated  : \
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…
Loops Section Overview

872 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