模式匹配返回零计数

时间:2017-09-03 11:42:37

标签: c# regex

我正在尝试为输入文本创建正则表达式模式匹配。虽然我肯定知道正则表达式有效: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;
        }
    }

1 个答案:

答案 0 :(得分:3)

您必须使用OR来组合RegexOptions:

RegexOptions.Multiline | RegexOptions.IgnoreCase