Link to home
Start Free TrialLog in
Avatar of rutledgj
rutledgj

asked on

vb.net Need regex to find suffix in name

Could someone show me how to use regular expression to look for common suffixes in a person's name?

Avatar of kaufmed
kaufmed
Flag of United States of America image

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.
Avatar of rutledgj
rutledgj

ASKER

Fine, Then how do you call the expression to return a true/false condition?
Be sure to turn on case-insensitivity. I forgot to demonstrate that above.

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

Open in new window

You can use the method I demonstrated immediately above for a true/false return.
Is it possible to use the expression to just remove it from the name to begin with?

MyName.Replace(pattern,"")?

Certainly:

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

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
Ok. Thanks. Big help.