public virtual DateTime?ParseAgeInYear(string inputAge) { int year = 0; int month = 0; string age = string.Empty; var ageByGender = Regex.Match(inputAge, @"(?<=בן)(.*?)(?=[\.\,])"); if (!ageByGender.Success) { ageByGender = Regex.Match(inputAge, @"(?<=בת)(.*?)(?=[\.\,])"); } if (ageByGender.Success) { age = ageByGender.Value; //age includes years and months if (age.Split(new string[] { " ו" }, StringSplitOptions.None).Length > 1) { string[] splitByAnd = age.Split(new[] { " ו" }, 2, StringSplitOptions.None); year = ParserUtils.ConvertYear(splitByAnd[0]); month = ParserUtils.ConvertMonth(splitByAnd[1]); } } else { age = inputAge; } if (age.Contains("חודש")) { //age includes only months month = ParserUtils.ConvertMonth(age); } else if (age.Contains("חצי שנה")) { month = ParserUtils.ConvertMonth("6"); } else if (Regex.IsMatch(inputAge, @"\b\d{4}\b")) { //age given in year (e.g. 2017) year = ParserUtils.ConvertYear(Regex.Match(inputAge, @"\b\d{4}\b").Value); } else { //age include only years year = ParserUtils.ConvertYear(age); } //in case of unvalid date of birth if ((year == 0 && month == 0) || year > 25) { return(null); } return(DateTime.Now.AddYears(-year).AddMonths(-month).Date); }
public void TestConvertYear() { string input = "שנתיים"; int result = ParserUtils.ConvertYear(input); Assert.IsTrue(result == 2); Debugger.Break(); }