private static void ParseRegionValue(string text, Regex valuePrefixRegex, Regex valuePostfixRegex, Regex valueRegex, Regex fullValueRegex, out StringSearchResult valuePrefixResult, out StringSearchResult valueResult, out StringSearchResult valuePostfixResult) { valuePrefixResult = StringSearchResult.Empty; valueResult = StringSearchResult.Empty; valuePostfixResult = StringSearchResult.Empty; valuePrefixResult = ParseFirstByRegexp(text, valuePrefixRegex, 0); if (valuePrefixResult.IsEmpty) { Match regexMatch = fullValueRegex.Match(text); if (regexMatch.Success) { valuePrefixResult = new StringSearchResult(regexMatch.Groups[1].Index, regexMatch.Groups[1].Value); valueResult = new StringSearchResult(regexMatch.Groups[2].Index, regexMatch.Groups[2].Value); valuePostfixResult = new StringSearchResult(regexMatch.Groups[3].Index, regexMatch.Groups[3].Value); } return; } int iValue = valuePrefixResult.Index + valuePrefixResult.Keyword.Length; valuePostfixResult = ParseFirstByRegexp(text.Substring(iValue, text.Length - iValue), valuePostfixRegex, 0).OffsetResult(iValue); valueResult = valuePostfixResult.IsEmpty ? StringSearchResult.Empty : ParseFirstByRegexp(text.Substring(iValue, valuePostfixResult.Index - iValue), valueRegex, 1).OffsetResult(iValue); }
private void ParseNamePersons(string trimText, out List <StringSearchResult> namePersonPrefixResult, out List <StringSearchResult> namePersonValueResult, out List <StringSearchResult> namePersonPostfixResult) { namePersonPrefixResult = new List <StringSearchResult>(); namePersonValueResult = new List <StringSearchResult>(); namePersonPostfixResult = new List <StringSearchResult>(); List <StringSearchResult> searchMatches = ParseAllByRegexp(trimText, namePersonPrefixRegex); if (searchMatches.Any()) { foreach (StringSearchResult namePersonPrefixMatch in searchMatches) { int iNamePersonValue = namePersonPrefixMatch.Index + namePersonPrefixMatch.Keyword.Length; StringSearchResult namePersonPostfixMatch = ParseFirstByRegexp(trimText.Substring(iNamePersonValue, ParsingTextHelper.NamePersonMaxLength), namePersonPostfixRegex, 0).OffsetResult(iNamePersonValue); string namePersonValueText = namePersonPostfixMatch.IsEmpty ? string.Empty: trimText.Substring(iNamePersonValue, namePersonPostfixMatch.Index - iNamePersonValue); StringSearchResult namePersonValueMatch = namePersonPostfixMatch.IsEmpty ? StringSearchResult.Empty : ParseFirstByRegexp(namePersonValueText, namePersonValueRegex, 1).OffsetResult(iNamePersonValue); if (!namePersonPostfixMatch.IsEmpty && namePersonValueMatch.IsEmpty) { namePersonValueMatch = new StringSearchResult(iNamePersonValue, namePersonValueText); } namePersonPrefixResult.Add(namePersonPrefixMatch); namePersonValueResult.Add(namePersonValueMatch); namePersonPostfixResult.Add(namePersonPostfixMatch); } } else { MatchCollection regexMatches = namePersonFullRegex.Matches(trimText); foreach (Match namePersonMatch in regexMatches) { var namePersonPrefixMatch = new StringSearchResult( namePersonMatch.Groups[1].Index, namePersonMatch.Groups[1].Value); var namePersonValueMatch = new StringSearchResult( namePersonMatch.Groups[2].Index, namePersonMatch.Groups[2].Value); var namePersonPostfixMatch = new StringSearchResult( namePersonMatch.Groups[3].Index, namePersonMatch.Groups[3].Value); namePersonPrefixResult.Add(namePersonPrefixMatch); namePersonValueResult.Add(namePersonValueMatch); namePersonPostfixResult.Add(namePersonPostfixMatch); } } }
private string Test1(string namePersonRegion, StringSearchResult prefixResult, StringSearchResult valueResult, StringSearchResult postfixResult) { try { return(valueResult.IsEmpty ? string.Empty : namePersonRegion.Substring(prefixResult.Index + prefixResult.Keyword.Length, valueResult.Index - (prefixResult.Index + prefixResult.Keyword.Length)) + "{0}" + namePersonRegion.Substring(valueResult.Index + valueResult.Keyword.Length, postfixResult.Index - (valueResult.Index + valueResult.Keyword.Length))); } catch (Exception e) { Console.WriteLine(e); throw; } }
public static StringSearchResult OffsetResult(this StringSearchResult result, int offset) { return(result.IsEmpty ? result : new StringSearchResult(result.Index + offset, result.Keyword)); }
public IEnumerable <ParseResult> ParseBySearch(string trimText) { List <StringSearchResult> namePersonPrefixResult; List <StringSearchResult> namePersonValueResult; List <StringSearchResult> namePersonPostfixResult; ParseNamePersons(trimText, out namePersonPrefixResult, out namePersonValueResult, out namePersonPostfixResult); if (namePersonValueResult.Any()) { for (int i = 0; i < namePersonPrefixResult.Count; i++) { StringSearchResult namePersonPrefixMatch = namePersonPrefixResult[i]; StringSearchResult namePersonValueMatch = namePersonValueResult[i]; StringSearchResult namePersonPostfixMatch = namePersonPostfixResult[i]; if (namePersonValueMatch.IsEmpty) { continue; } int namePersonRegionLength = i + 1 < namePersonPrefixResult.Count ? namePersonPrefixResult[i + 1].Index - namePersonPrefixMatch.Index : Math.Min(namePersonPrefixMatch.Keyword.Length + namePersonValueMatch.Keyword.Length + ParsingTextHelper.NamePersonRegionMaxLength, trimText.Length - namePersonPrefixMatch.Index); string namePersonRegion = trimText.Substring(namePersonPrefixMatch.Index, namePersonRegionLength); StringSearchResult aggregatedAmountPrefixResult; StringSearchResult aggregatedAmountValueResult; StringSearchResult aggregatedAmountPostfixResult; ParseRegionValue(namePersonRegion, aggregatedAmountPrefixRegex, aggregatedAmountPostfixRegex, aggregatedAmountValueRegex, aggregatedFullAmountRegex, out aggregatedAmountPrefixResult, out aggregatedAmountValueResult, out aggregatedAmountPostfixResult); StringSearchResult percentOwnedPrefixResult; StringSearchResult percentOwnedValueResult; StringSearchResult percentOwnedPostfixResult; ParseRegionValue(namePersonRegion, percentOwnedPrefixRegex, percentOwnedPostfixRegex, percentOwnedValueRegex, percentOwnedFullRegex, out percentOwnedPrefixResult, out percentOwnedValueResult, out percentOwnedPostfixResult); yield return(new ParseResult { NamePersonPrefix = namePersonPrefixMatch.Keyword, NamePersonValue = namePersonValueMatch.Keyword, NamePersonPostfix = namePersonPostfixMatch.Keyword, NamePerson = Test(trimText, namePersonPrefixMatch, namePersonPostfixMatch), NamePersonTest = Test1(trimText, namePersonPrefixMatch, namePersonValueMatch, namePersonPostfixMatch), AggregatedAmountPrefix = aggregatedAmountPrefixResult.Keyword, AggregatedAmountValue = aggregatedAmountValueResult.Keyword, AggregatedAmountPostfix = aggregatedAmountPostfixResult.Keyword, AggregatedAmount = Test(namePersonRegion, aggregatedAmountPrefixResult, aggregatedAmountPostfixResult), PercentOwnedPrefix = percentOwnedPrefixResult.Keyword, PercentOwnedValue = percentOwnedValueResult.Keyword, PercentOwnedPostfix = percentOwnedPostfixResult.Keyword, PercentOwned = Test(namePersonRegion, percentOwnedPrefixResult, percentOwnedPostfixResult), Region = namePersonRegion.Substring(0, namePersonRegion.LastIndexOf(' ')) }); } } else { yield return(new ParseResult()); } }