RegEx expression leave out specific word at end if present

Hi Experts

I need a RegEx expression that leaves out a word if it is present at the end of the string, e.g. if the string ends with Ave. it should not be included where as if it starts with Ave. it should:

Drive Ave. should return Drive
Ave. Drive should return Ave. Drive
Ave. Drive Ave. should return Ave. Drive

I seem to struggle with the RegEx for it to handle the examples in one go.


How about the following expression?

System.Text.RegularExpressions.Regex.Match(yourString,"(Ave\. Drive|Drive)").Value


1 Like

Hi Yoichi

“Drive” is not static in the string. “Ave.” should always be left out if it exists last in the string. But if the string starts with “Ave.” it should be included.


System.Text.RegularExpression.Regex.Replace(myString, "\W*Ave$", "")