【问题标题】:Regex: Invert Match order正则表达式:反转匹配顺序
【发布时间】:2011-12-03 12:20:29
【问题描述】:

我有这个 sn-p 来执行正则表达式搜索:

public IEnumerable<MyMatch> GetMyMatches() 
{
   Match m = myRegex.Match(Text, offset);
   if (m != null && m.Success && m.Value != null && m.Value.Length > 0)
   {
      offset = m.Index+m.Length;
      yield return new MyMatch() { Match=m, SomeFurtherInformation=... };
   } else
   yield break;
}

如您所见,我遍历了文本中的所有出现。

但是如何反转搜索方向?

感谢您的帮助

【问题讨论】:

  • 注意:m != null 始终为真,m.Value != null 也始终为真,除非有异常(如偏移量太大)但没有 m :-) 如果有不匹配,值 == ""。

标签: c# regex search ienumerable


【解决方案1】:

您可以使用“Matches”,然后对返回的 IEnumerable 执行“Reverse”。

【讨论】:

【解决方案2】:

RegexOptions 中有一个 RightToLeft 选项 - 你可能也需要调整你的表达方式,但这会为你“向后”搜索。

【讨论】:

    猜你喜欢
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多