搜索文件时,只返回带有重复项的字符串

时间:2015-12-08 13:55:43

标签: c# regex string csv

目前正在开展一个停车场项目。我有一个csv文件,其中包含有关汽车牌照的信息字符串。我已经使用正则表达式来返回90%的车牌,但是更短的个性化车牌并没有正确地返回:即#34; AA12"返回" AA12BC"因为它适合另一个正则表达式。

每个字符串都有两个汽车牌照实例,有没有办法只返回证明正确的正则表达式和两个号牌实例的字符串。

到目前为止

代码:

//启动

        using (TextReader reader = File.OpenText(@"C:\Users\user\documents\regdata.csv"))
        {
            List<string> lines = new List<string>();
            string pattern = @"[A-Z]{3}[0-9]{3}";
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                lines.Add(line);
            }

            List<string> regExs = new List<string>();

            regExs.Add(@"[A-Z]{3}[0-9]{3}");
            regExs.Add(@"[A-Z]{2}[0-9]{2}[A-Z]{3}");
            regExs.Add(@"[A-Z]{1}[0-9]{3}[A-Z]{3}");
            regExs.Add(@"[A-Z]{1}[0-9]{2}[A-Z]{3}");
            regExs.Add(@"[A-Z]{1}[0-9]{1}[A-Z]{3}");
            regExs.Add(@"[A-Z]{3}[0-9]{2,3}");
            regExs.Add(@"[A-Z]{2}[0-9]{4}");
            regExs.Add(@"[A-Z]{3}[0-9]{2}");
            regExs.Add(@"[A-Z]{2}[0-9]{2}[A-Z]{3}");





                using (StreamWriter writer = new StreamWriter(@"C: \Users\user\Desktop\usersNotes\plates.csv"))
            {

                foreach (var l in lines.Select(x => x.Split(',')[2]))
                {

                    string result = "";
                    foreach (var r in regExs)
                    {

                        Regex myRegex = new Regex(r);

                        Match m = myRegex.Match(l);
                        if (m.Success)
                        {
                            result = m.Value;
                            break;
                        }
                    }


                    writer.WriteLine(l + "," + result);

                }

由于

1 个答案:

答案 0 :(得分:1)

这应该为你做。根据我的理解,我继续编码(我认为)整个解决方案。我制作了一个正则表达式而不是字符串列表;通过这种方式,您不必在每个循环中构建和拆除每个Regex对象。

假设:(1)板块永远不会有&#34;或者,和(2)印版不会出现两次以上。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace DupeOnly {
public partial class Form1 : Form{
   public Form1(){
      InitializeComponent();
   }
   private void button1_Click(object sender, EventArgs e){
      string zRegData = File.ReadAllText(@"C:\Users\user\documents\regdata.csv");
      HashSet<string> hsRegData = new HashSet<string>(); 

  bool tfFirst = true;

  string[] zAllPlateData = zRegData.Split(',');  //License plates don't have comma's

  List<Regex> rxList = new List<Regex>();
  rxList.Add(new Regex(@"[A-Z]{3}[0-9]{3}"));
  rxList.Add(new Regex(@"[A-Z]{2}[0-9]{2}[A-Z]{3}"));
  rxList.Add(new Regex(@"[A-Z]{1}[0-9]{3}[A-Z]{3}"));
  rxList.Add(new Regex(@"[A-Z]{1}[0-9]{2}[A-Z]{3}"));
  rxList.Add(new Regex(@"[A-Z]{1}[0-9]{1}[A-Z]{3}"));
  rxList.Add(new Regex(@"[A-Z]{3}[0-9]{2,3}"));
  rxList.Add(new Regex(@"[A-Z]{2}[0-9]{4}"));
  rxList.Add(new Regex(@"[A-Z]{3}[0-9]{2}"));
  rxList.Add(new Regex(@"[A-Z]{2}[0-9]{2}[A-Z]{3}"));
  Match m;

  using (StreamWriter sw = new StreamWriter(@"C: \Users\user\Desktop\usersNotes\plates.csv")){
     for(int Q = 0; Q < zAllPlateData.Length; Q++){
        if(hsRegData.Add(zAllPlateData[Q]) == false){

           //At this point we know it is a duplicate, must still match a check pattern
           foreach(Regex rx in rxList){
              m = rx.Match(zAllPlateData[Q]);
              if(m.Success){

                 if(tfFirst){
                    tfFirst = false;

                    sw.Write(zAllPlateData[Q]);  //First plate doesn't take a comma
                 }
                 else{
                    sw.Write("," + zAllPlateData[Q]);  //Comma delimit subsequent plates
                 }
                 break;
              }
           }
        }
     }
      }
   }
}
}