csharp regex显示文本文件中的匹配模式 [英] csharp regex display matched pattern from text file

查看:123
本文介绍了csharp regex显示文本文件中的匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我在正则表达式中使用正则表达式吗...
我想做的是显示所有包含文本文件中图案的行,
在阅读了很多tuto并使用了Regex Tester等工具之后,什么都没有了.
-File_Info.txt是目标文件,其中包含带有单词"01234567_TRYN_YZ"的行
-要显示的行示例:05/23/12 19:31:20:dsetup32 \ 01234567_TRYN_YZ.txt停止执行...
提前谢谢.

这是我的代码:

字符串[]行= File.ReadAllLines("File_Info.txt");
正则表达式regex = new Regex("^ [0-9] +.[T] \ w {3}.[Y] \ w {1} $");
foreach(行中的字符串行)
{
匹配match = regex.Match(line.Trim())
如果(匹配成功)
{
Console.WriteLine(Line);
}
}

Can someone help me with Regex in csharp...
What I"m trying to do is displaying all lines containing a pattern from a text file,
after reading a lot of tuto and using tools like Regex Tester etc... nothing is working.
- File_Info.txt is the target file containing lines with word "01234567_TRYN_YZ"
- example of line to be displayed: 05/23/12 19:31:20: dsetup32\01234567_TRYN_YZ.txt stop to do...
Thanks in advance.

This is my code:

string[] lines = File.ReadAllLines("File_Info.txt");
Regex regex = new Regex("^[0-9]+.[T]\w{3}.[Y]\w{1}$");
foreach (string line in lines)
{
Match match = regex.Match(line.Trim())
if (match.Successful)
{
Console.WriteLine(Line);
}
}

推荐答案

);
foreach(行中的字符串行)
{
匹配match = regex.Match(line.Trim())
如果(匹配成功)
{
Console.WriteLine(Line);
}
}
");
foreach (string line in lines)
{
Match match = regex.Match(line.Trim())
if (match.Successful)
{
Console.WriteLine(Line);
}
}




我对代码进行了一些修改,并且可以正常工作

Hi,

I have modified the code a bit and its working

string[] lines = File.ReadAllLines("d:\\milind.txt");
Regex regex = new Regex(@"\d{8}_TRYN_YZ");
foreach (string line in lines)
{
    Match match = regex.Match(line.Trim());
    if (match.Success)
    {
        Console.WriteLine(line);
    }
}



我在文件中使用了以下数据.



I have used following data in the file.

05/23/12 19:31:21: dsetup32\01234567_TRYN_YZ.txt
05/23/12 19:31:22: dsetup32\_TRYN_YZ.txt
05/23/12 19:31:23: dsetup32\01234567__YZ.txt
05/23/12 19:31:24: dsetup32\01234533_TRYN_YZ.txt
05/23/12 19:31:25: dsetup32\01234544_TRYN_YZ.txt



并正确显示结果为



And it displayed result correctly as

05/23/12 19:31:21: dsetup32\01234567_TRYN_YZ.txt
05/23/12 19:31:24: dsetup32\01234533_TRYN_YZ.txt
05/23/12 19:31:25: dsetup32\01234544_TRYN_YZ.txt



希望有帮助.

Milind



Hope that helps.

Milind


这篇关于csharp regex显示文本文件中的匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆