读取XML并通过正则表达式进行匹配

配置表的格式如下:


输入一个字符串,通过正则表达式与配置表中的信息进行匹配:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Collections;
using System.Text.RegularExpressions;


namespace XML
{
    class Program
    {
        // 配置表中各个节点的信息
        private class NodeInfo
        {
            //Name
            public String Name { get; set; }

            //Path
            public String Path { get; set; }

            //Subpath
            public String Subpath { get; set; }

            //Iseff
            public String Iseff { get; set; }
        }

        //配置表信息列表
        private List<NodeInfo> m_nodeInfoList = new List<NodeInfo>();
        private string m_xmlFilePath = "\\输出路径配置表.xml";
        private bool m_isEff = false;
        
        //获取配置表中信息
        private void ReadExporterConfigurationInfoFromFile()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Directory.GetCurrentDirectory() + this.m_xmlFilePath);
            this.m_nodeInfoList.Clear();
            foreach (XmlNode node in xmlDoc.SelectSingleNode("rules").SelectNodes("rule"))
            {
                NodeInfo newNode = new NodeInfo();
                newNode.Name = node.Attributes["name"].Value;
                newNode.Path = node.Attributes["path"].Value;
                newNode.Subpath = node.Attributes["subpath"].Value;
                newNode.Iseff = node.Attributes["iseff"].Value;
                this.m_nodeInfoList.Add(newNode);
            }
        }

        //进行匹配
        private bool CanMatch(string str)
        {
            string RegexStr = string.Empty;
            foreach (NodeInfo n in m_nodeInfoList)  //匹配路径
            {
                RegexStr = n.Name;
                if (Regex.IsMatch(str, RegexStr))
                {
                    this.m_isEff = n.Iseff == "true" ? true : false;
                    return true;
                }
            }
            return false;
        }

        static void Main(string[] args)
        {
            string str = "c12345";   // 需要进行匹配的字符串
            Program p = new Program();
            //获取配置表信息
            p.ReadExporterConfigurationInfoFromFile();
            // 匹配字符串
            bool isMatch = p.CanMatch(str);
            if (isMatch)
            {
                Console.Write("匹配成功!");
            }
            else
            {
                Console.Write("匹配失败!");
            }
        }
    }
}
如:

当str=“c12345”,与name="^c\d{5}$"匹配成功,输出“匹配成功!”


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值