[英]I'm trying to read a Regex from an xml file but when i pass on the Regex to the C# code but I'm getting false matches
When i give my Regex by Xml I'm getting False matches but when I hardcode the Regex in the C# code I'm getting the right Matches , please help me out.当我通过 Xml 提供我的 Regex 时,我得到了 False 匹配,但是当我在 C# 代码中对 Regex 进行硬编码时,我得到了正确的 Matches ,请帮助我。
The code was working fine when I directly put the Regex in the Code.当我直接将 Regex 放入代码时,代码运行良好。 The problem started when I began to use XMl file
当我开始使用 XML 文件时,问题就开始了
public static List<string> RegxFind(string path, string XmlPath)
{
string Patterns = "";
XmlReader reader = XmlReader.Create(XmlPath);
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name.ToString())
{
case "reg1":
Console.WriteLine(reader.Value);
Console.ReadKey();
Patterns = reader.Value;
break;
}
}
}
List<string> Results = new List<string>();
Excel.Application xlapp = new Excel.Application();
Excel.Workbooks xlworkbooks = xlapp.Workbooks;
//string Pattern = @"\b[0-9DEF]{2}[A-z]{ 2}[0-9]{6}[0-9]{3}|[0-9DEF]{2}[A-z]{2}[0-9]{6}[\S\s][0-9]{3}|[0-9DEF]{2}[A-z]{2}[0-9]{6}[\S\s][\s][0-9]{3}|[0-9DEF]{2}[A-z]{2}[\s\W\d]{8,12}\b";
try
{
xlapp = new Excel.Application();
xlapp.Visible = true;
xlworkbooks = xlapp.Workbooks;
Excel.Workbook xlworkbook = xlworkbooks.Open(path);
Excel.Sheets xlsheets = xlworkbook.Worksheets;
Excel._Worksheet xlsheet = xlworkbook.Sheets[1];
Excel.Range xlRange = xlsheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
if (xlRange.Cells[i, j].value != null)
{
//Console.WriteLine("vlues" + xlRange.Cells[i, j].Value);
//Console.ReadKey();
string tempstr = Convert.ToString((xlRange.Cells[i, j].Value));
Match m = Regex.Match(tempstr, Patterns);
if (m.Success)
{
Results.Add((xlRange.Cells[i, j].Value) + "<>");
tempstr = "";
}
else
{
tempstr = "";
m.NextMatch();
}
}
else
{
continue;
}
}
}
xlworkbook.Close();
xlapp.Quit();
return Results;
}
catch (Exception es)
{
Console.WriteLine("error:" + es);
Console.ReadKey();
return Results;
}
}
The problem is , when I'm reading from the xml file I'm unable to get proper match as the Pattern variable is not receiving any value at line Regx.match(tempstr,patterns) I want The code to give me correct pattern Matches here the xml for reference :问题是,当我从 xml 文件中读取时,我无法获得正确的匹配,因为 Pattern 变量在 Regx.match(tempstr,patterns) 行没有收到任何值我想要代码给我正确的模式匹配这里的 xml 供参考:
<?xml version="1.0" encoding="UTF-8"?>
<pattern>
<reg1>@"\b[0-9DEF]{2}[A-z]{ 2}[0-9]{6}[0-9]{3}|[0-9DEF]{2}[A-z]{2}[0-9]{6}[\S\s][0-9]{3}|[0-9DEF]{2}[A-z]{2}[0-9]{6}[\S\s][\s][0-9]{3}|[0-9DEF]{2}[A-z]{2}[\s\W\d]{8,12}\b"</reg1>
</pattern>
First, fix your pattern a bit: 1) group the alternatives so that word boundaries could be applied to all the alternatives, 2) replace [Az]
with [A-Za-z]
to match just ASCII letters and 3) remove spaces inside limiting quantifiers.首先,稍微修正一下您的模式:1) 将备选方案分组,以便单词边界可以应用于所有备选方案,2) 将
[Az]
替换为[A-Za-z]
以仅匹配 ASCII 字母和 3) 删除内部的空格限制量词。
So, it should at least look like所以,它至少应该看起来像
\b(?:[0-9DEF]{2}[A-Za-z]{2}[0-9]{6}[0-9]{3}|[0-9DEF]{2}[A-Za-z]{2}[0-9]{6}[\S\s][0-9]{3}|[0-9DEF]{2}[A-Za-z]{2}[0-9]{6}[\S\s][\s][0-9]{3}|[0-9DEF]{2}[A-Za-z]{2}[\s\W\d]{8,12})\b
You may further contract it by grouping it further as the [0-9DEF]{2}[A-Za-z]{2}
starts all the alternatives:当
[0-9DEF]{2}[A-Za-z]{2}
启动所有备选方案时,您可以通过将其进一步分组来进一步收缩它:
\b[0-9DEF]{2}[A-Za-z]{2}(?:[0-9]{6}(?:[0-9]{3}|[\S\s]\s?[0-9]{3})|[\s\W\d]{8,12})\b
See the regex demo .请参阅正则表达式演示。
Put it into a Patterns.xml
file as a string inside a CDATA
block to avoid having to escape chars:将其作为
CDATA
块内的字符串放入Patterns.xml
文件中,以避免转义字符:
<?xml version="1.0" encoding="UTF-8"?>
<pattern>
<reg1><![CDATA[\b[0-9DEF]{2}[A-Za-z]{2}(?:[0-9]{6}(?:[0-9]{3}|[\S\s]\s?[0-9]{3})|[\s\W\d]{8,12})\b]]></reg1>
</pattern>
Read it:阅读:
public static string RegxFind(string XmlPath)
{
var xml = XDocument.Load(XmlPath);
return xml.Root.Descendants("reg1").FirstOrDefault()?.Value;
}
Then, read it into any variable, static or not:然后,将其读入任何变量,无论是否为静态:
var pattern = RegxFind(path_to_xml);
and use the pattern.并使用该模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.