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.

Hi,

How about the following expression?

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

Regards,

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.

Hello,

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