0

I need to see if my address contains certain fractions, but not all. Addresses like Hwy 1/158 should not match. For now I'm just matching to 1/4, 1/2, 3/4. I may add to the list later depending on data, but for now that will handle the vast majority of my issues.

Currently I'm just testing for "1/2" like so

if (Address.Contains("1/2"))
{
    Address = Address.Replace("1/2", "");
    Fraction = "1/2";
}

How can I match for multiple fractions, remove those fractions, save the fraction match to later be assigned to Fraction?

user2197446
  • 1,065
  • 3
  • 15
  • 31
  • Make a list of fractions that you want to replace, search for each of them, and replace/save it when you find one. – D Stanley Nov 07 '14 at 15:27

1 Answers1

1

You can use Regex.Replace method to match multiple patterns and use its evaluator to save matched element:

var fractions = new[] { "1/2", "1/4", "3/4" };
var address = "Street 1/4";
List<string> matched = new List<string>();
string replaced = Regex.Replace(address, string.Join("|", fractions), match =>
{
    matched.Add(match.Value);;
    return string.Empty;
});
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58