配置表的格式如下:
输入一个字符串,通过正则表达式与配置表中的信息进行匹配:
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}$"匹配成功,输出“匹配成功!”