【问题标题】:Pattern match returns zero count [duplicate]模式匹配返回零计数
【发布时间】:2017-09-03 19:50:38
【问题描述】:

我正在尝试为输入文本创建正则表达式模式匹配。虽然我确定正则表达式有效:https://regex101.com/r/EU7fXK/1 我无法使用代码创建匹配项(计数 > 0),即它不返回匹配项。不知道是什么原因。

我的代码是这样的

 static class Program
    {
        public static Regex Device { get; } = new Regex(@"\bdevice.*\b\W", RegexOptions.IgnoreCase & RegexOptions.Multiline);
        static void Main(string[] args)
        {
            var src =
                "!######################################################################################################\r\n!# APPROVED : YES\r\n!# REASON: test1 hosts in DMZ test1x access to update manager server to maintain securMuramaty, C1TEST1TEST1XX, test1xxx\r\n!# DEVICE: xxx test1 Devices\r\n!# SECTION: test1\r\n!# REQUESTER: test1@test1test2.com\r\n!# Exp. Date : 8/2016\r\n\r\naccess-list <Rule No> permMuramat tcp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>\r\naccess-list <Rule No> permMuramat tcp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>\r\naccess-list <Rule No> permMuramat tcp <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>\r\naccess-list <Rule No> permMuramat <IPaddress> <subnet mask> <IPaddress> <subnet mask> eq <Port Number>\r\n";

            var devices = src.GetDevice();

        }

        public static List<XElement> GetDevice(this string input)
        {
            var pattern = Device;

            var matchCollection = pattern.Matches(input);

            var elements = new List<XElement>();

            foreach (Match match in matchCollection)
            {
                var val = match.Value;

                var element = new XElement("Device") { Value = val };

                elements.Add(element);
            }
            return elements;
        }
    }

【问题讨论】:

    标签: c# regex


    【解决方案1】:

    您必须使用 OR 来组合 RegexOptions:

    RegexOptions.Multiline | RegexOptions.IgnoreCase
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      相关资源
      最近更新 更多