?
Solved

vb.net Need regex to find suffix in name

Posted on 2011-10-19
8
Medium Priority
?
359 Views
Last Modified: 2012-05-12
Could someone show me how to use regular expression to look for common suffixes in a person's name?

0
Comment
Question by:rutledgj
  • 5
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36994720
Do you mean like "Ph.D.", "Jr.", "Sr.", etc.? If so, here is a start:

Dim pattern As String = "ph\.d\.|jr\.|sr\.|Atty"

Open in new window


You can add more suffixes by inserting a pipe ( | ) and then the suffix. Make sure you escape any dots with a backslash like I did above.
0
 

Author Comment

by:rutledgj
ID: 36994733
Fine, Then how do you call the expression to return a true/false condition?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36994734
Be sure to turn on case-insensitivity. I forgot to demonstrate that above.

Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase)

Open in new window

0
Independent Software Vendors: 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!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36994770
You can use the method I demonstrated immediately above for a true/false return.
0
 

Author Comment

by:rutledgj
ID: 36994784
Is it possible to use the expression to just remove it from the name to begin with?

MyName.Replace(pattern,"")?

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36994892
Certainly:

Regex.Replace(input, pattern, String.Empty, RegexOptions.IgnoreCase)

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 36994933
Also, be careful with which suffixes you add to your pattern. I demonstrated "Atty" (short for attorney) in the pattern above, but what happens if the name you are inspecting is "Patty". You will effectively change the person's name to "P". You can try adding word boundaries around the pattern to help mitigate this:

Dim pattern As String = "\b(?:ph\.d\.|jr\.|sr\.|Atty)\b"

Open in new window

0
 

Author Comment

by:rutledgj
ID: 36994952
Ok. Thanks. Big help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

864 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