C# 正则表达式 - 使用前检查/编辑匹配组

标签 c# regex

我在编辑正则表达式匹配组时遇到问题。

我想检查最后一个匹配组中是否有 1 位或 2 位数字,如果有 1 位数字,则在其前面添加 0。

代码示例:9/05/2015 我想要的示例:2015 05 09

代码:

string line;
string pattern = "([0-9]{1,2})/([0-9]{2})/([0-9]{4})";
string replacement = "$3 $2 $1";

System.IO.StreamReader file = new System.IO.StreamReader(path);
while ((line = file.ReadLine()) != null)
{
   Regex rgx = new Regex(pattern);
   string result = rgx.Replace(line, replacement);
   all += result + "\r\n";
   richTextBox1.Text += result + "\r\n";
}

感谢您的帮助

private void button2_Click_1(object sender, EventArgs e)
        {
            string path = textBox1.Text;
            string newFileName = DateTime.Now.ToString("d-M-yyyy hh,mm,ss");
            textBox2.Text = "data-"+newFileName+".txt";
            string text = System.IO.File.ReadAllText(path);

            string line;
            string pattern = "([0-9]{1,2})/([0-9]{2})/([0-9]{4})";
            string replacement = "$3 $2 $1";

            System.IO.StreamReader file = new System.IO.StreamReader(path);
            while ((line = file.ReadLine()) != null)
            {
                Regex rgx = new Regex(pattern);
                string result = rgx.Replace(line, replacement);
                all += result + "\r\n";
                richTextBox1.Text += result + "\r\n";
            }

            file.Close();
        }

文件看起来像:

ACD 000     9/04/2015   14:00 



REPT 1
ACD      AVG CALLS       AVG  AVG  AVG  AVG  AVG    DN  AVG   #-XFER  AVG-TIME-POSN 
 DN     AGTS ANSWD  ASA  DCP HDCP  PCP WORK WAIT CALLS TIME  IDN  ACD   BUSY MANNED
7777       1     3   15   91    0   82  173  477     4   96    0    0    453   1169 
-----------------------------------------------------------------------------------
  1              3   15   91    0   82  173  477     4   96    0    0    453   1169 


REPT 2
ACD     CALLS  RECALL ANSWERED  ABANDONED      TOF TOF  OVER    INTER  
 DN    ACCPTED  TO    LONGEST   NO. AVG.WT TSF IN  OUT  FLOW    FLOW   
               SOURCE WT. TIME                                  BUSY
7777         3      0       32    0  ****   66   0   0     0       0 
------------------------------------------------------------------------------
  1          3      0       32    0  ****   66   0   0     0       0 

        CALLS    CALLS ANSWERED      ABANDONED        ROUTE               DFLT
CDN    ACCPTED   NO.  ASA  LONG WT   NO. AVG.WT TSF     BY     DISC BUSY   DN 
                                                     IVR  CCR 
8830         0     0 ****        0     0  ****    0    0    0     0    0    0 

最佳答案

使用 DateTime 是正确的选择。作为一个选项,您可以将字符串解析为 int,并用 :Dn notation 填充零。在 string.Format() 中(这是安全的,因为您只捕获第一组中的数字):

// Declare somewhere before, better as a private readonly field
private readonly Regex rgx = new Regex("([0-9]{1,2})/([0-9]{2})/([0-9]{4})", RegexOptions.Compiled);

然后,在调用者内部:

var line1 = "9/05/2015";
if (rgx.IsMatch(line1))
{
    var result = string.Format("{0} {1} {2:D2}", rgx.Match(line1).Groups[3].Value, rgx.Match(line1).Groups[2].Value, Int32.Parse(rgx.Match(line1).Groups[1].Value));
}

输出:

enter image description here

编辑

我建议在读取文件时使用using,它会更优雅地释放资源。这是我的实现:

using (System.IO.StreamReader file = new System.IO.StreamReader(path))
{
   while ((line = file.ReadLine()) != null)
   {
       // Regex rgx = new Regex(pattern); // DECLARE IT BEFORE WITH Compiled OPTION
       Match m = rgx.Match(line);
       if (m.Success)
       {
           string result = string.Format("{0} {1} {2:D2}", m.Groups[3].Value, m.Groups[2].Value, Int32.Parse(m.Groups[1].Value));
           all += line.Replace(m.Value, result) + "\r\n";
           richTextBox1.Text += line.Replace(m.Value, result) + "\r\n";
       }
       else
       {
           all += line + "\r\n"; 
           richTextBox1.Text += line + "\r\n";
       }
   }
   file.Close();
}

关于C# 正则表达式 - 使用前检查/编辑匹配组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29624165/

相关文章:

c# - 在 LINQ 和 C# 中从 XML 文件中检索节点值

c# - 如何防止property setter修改私有(private)属性数据

带有 Windows 应用程序的 C# Web 服务

c# - UI的并行生成

javascript - 用 RegEx 和 jQuery 替换文本

Python正则表达式匹配字符串末尾的标点符号

Javascript 正则表达式问题 : strip special symbols and URL's

ruby-on-rails - 正则表达式匹配 Ruby 中成百上千的值

python - django 在列表中使用正则表达式排除 url

c# - 如何使用 C# 以编程方式定位我的 Dropbox 文件夹?