简体   繁体   English

我正在尝试从 xml 文件中读取正则表达式,但是当我将正则表达式传递给 C# 代码时,却得到了错误匹配

[英]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.

相关问题 我正在尝试从C#中的自定义文件类型读取 - I'm trying to read from a custom file type in C# 我正在尝试使用C#创建与Trac Wiki兼容的简单Wiki解析器,但是正则表达式使我丧命 - I'm trying to create simple wiki parser in C# that would be compatible with trac wiki, but regex is killing me 在 c# 中,我正在寻找一个只匹配第一次出现的正则表达式 - In c# I'm looking for a regex that will only match the first occurrence C#Visual Studio - 我正在尝试读取文本文件并将其显示在Richtextbox中并包含新行 - C# Visual Studio - I'm trying to read a text file and display it into Richtextbox and include new lines 尝试解析xml文件时,为什么我没有任何结果? - When trying to parse xml file why i'm getting no results at all? 我正在尝试使用 c# 文件句柄,但它不可用 - I'm trying to use the c# file handle, but it's not available 我试图简单地将 append 两种不同的数据类型放入我的 xml 文件并再次读取它们,但我收到错误 - I'm trying to simply append two different data types into my xml file and read them again, but I am getting an error 我正在尝试使用正则表达式捕获字符串。 以下是我试图从中提取数据的文本文件示例 - I'm trying to capture a string using regex. the following is a sample of the text file im trying to extract the data from C#我从Task或Dispatcher得到null - C# I'm getting null from Task or Dispatcher 我在使用C#中的多行正则表达式时遇到问题,如何解决此问题? - I'm having trouble with a multiline regex in C#, how do I fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM