Link to home
Start Free TrialLog in
Avatar of ITguy565
ITguy565Flag for United States of America

asked on

Regex query Problems

Experts,

I am fairly new to regex and need to write a query that will match names:

This query needs to be very strict and only match as closely as possible the following names and permenentations:

so far what I have is the following:
^From:\s\D((R|r)ic\w+)\s((\w\.|\w)\s)?((D|d)avi)?(\s(\w\.|\w)\s*)?

Open in new window


From: "Rickk L Davi"
From: "Rickk L. Davi"
From: "Rickk Davi L"
From: "Rickk Davi L."
From: "Rickk "

so far from the regex expression above I can match all of the information from the above test strings. However, I am getting quite a few false positives using this as it is also matching the first name without the middle initial and last name. Can this string be adapted where it will only match if it finds the First, Last Mi, First, MI, Last, First Last and NOT match on First name only or First, MI.


If possible can you explain how the query functions if significantly different from my above, as I would love to continue getting better developing regular expressions.


Thanks in advance for your assistance with this:
-ITGUY565
EXPERT CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ITguy565

ASKER

oBdA:

Thanks as always for your assistance with this..

This is perfect other than 1 match.

The query above matches the first name by itself, I am only looking at matching the following combinations:

First, Last
First,MI,Last
First Last, MI

In reality, I would also like to match the inverse of this :
Last First
Last, First MI
Last MI First
But since that wasn't in my original Question if need be I will open a new one.

I definitely don't want to match the First name by itself though, as it is currently throwing too many False Positives in my spam system.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oBdA,

That was just what I needed.. On a side note, I am masking the name I am actually searching for due to the sensitivity of the name within my organization. The gentleman's name is not Rickk which is why I am getting the false positives :P and I will further modify the string hopefully to adapt for nicknames shortly.

Thanks again for your assistance as this was driving me absolutely crazy!

Anychance you would be willing to assist with the second part of this question?

In reality, I would also like to match the inverse of this :
Last First
Last, First MI
Last MI First
But since that wasn't in my original Question if need be I will open a new one.

or would you like me to open a new one? I understand both ways !
Thanks as always for your assistance oBdA you have been instrumental in my knowledge on Experts-Exchange in powershell as well as Regular Expressions.
EXPERT CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oBdA,

you are correct, I am not utilizing this in powershell, I am using this for a impersonation protection policy within my E-mail spam filter.
Avatar of oBdA
oBdA

Does that filter support the "Ignore Whitespace" option? if it accepts the "Case Insensitive" option (the i in (?ix)), chances aren't bad.
When you paste the first code block into a PS console, you can just use
$pattern | clip.exe
to get the pattern into the clipboard.
Otherwise, if it'll all has to be in one line, I could change it so that you can edit and test it in PS in a more or less readable format, and PS will create the pattern for the filter.
I believe it has to be one line for my spam system to filter it properly.
EXPERT CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That Pattern works Excellent in Regex101.. I will give it a try in the spam filter momentarily.

Thanks again for your assistance oBdA! you always go above and beyond on your questions!
The spam filter was able to see the regex expression.. Just need to test at this point. Thanks for your help !
oBdA,

One Last thing if you wouldn't mind helping.. I noticed that the header for Rickk Davi and Rickk L. Davi are different..

Rickk Davi has a header of
From: Rickk Davi

Open in new window

Rickk L. Davi has a header of
From: "Rickk L. Davi"

Open in new window


I can't figure out how to add the changes to your powershell script above.
EXPERT CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are the man!!! I have been trying to figure that out for the last hour!