/// <summary> /// テキスト全体でマッチ箇所を全て列挙 /// </summary> /// <param name="str"></param> /// <param name="context"></param> /// <returns></returns> public IEnumerable <RegexMatch> Matches(StringPointer str, MatingContext context) { for (int i = 0; i < str.Length + 1; i++) { var subStr = str.SubString(i); foreach (var match in HeadMatches(subStr, new MatingContext())) { yield return(match); } } }
/// <summary> /// 範囲重複を許容せず最高優先マッチ箇所をすべて列挙 /// </summary> /// <param name="str"></param> /// <returns></returns> public IEnumerable <RegexMatch> MatchesRegular(StringPointer str) { for (int i = 0; i < str.Length + 1; i++) { var subStr = str.SubString(i); var match = HeadMatch(subStr, new MatingContext()); if (match != null) { yield return(match); i += Math.Max(match.Length - 1, 0); } } }
/// <summary> /// 文字列の先頭から、後続を考慮したマッチを優先度順に列挙 /// </summary> /// <param name="str"></param> /// <param name="context"></param> /// <returns></returns> public IEnumerable <RegexMatch> HeadMatches(StringPointer str, MatingContext context) { if (_nextRegex == null)//末端は自身のマッチを返せばいい。 { foreach (var matching in SimpleMatchings(str, context)) { yield return(matching); } } else//末端以外は、自分以降にマッチが存在するマッチを返す。 { foreach (var selfMatch in SimpleMatchings(str, context)) { var nextStr = str.SubString(selfMatch.Length); foreach (var nextMatch in _nextRegex.HeadMatches(nextStr, context)) { yield return(new ArrayMatch(str, selfMatch, nextMatch)); } } } }