简体   繁体   中英

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.

The code was working fine when I directly put the Regex in the Code. The problem started when I began to use XMl file

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 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.

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:

\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:

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM