如何将几个正则表达式用于一个方法,C# [英] How to use several regex into a method , C#

查看:62
本文介绍了如何将几个正则表达式用于一个方法,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的应用程序使用几个正则表达式,在阅读源文本文件中的所有行后,只写入包含student-s数据的行。之后我还要删除一些字符并拆分:(成员12569是学生 - 12 * 01 * 2006)分为5个字段,如下所示:



输入文本文件

表格| 01; 23_anna-会员12569是学生 - 12 * 01 * 2006

表格| 02; 17_smith_会员12570是老师 - 13 * 01 * 2007

表格| 03; 12_ben_会员12571是学生 - 14 * 01 * 2007



到目前为止在输出文件上打印的内容:

表格| 01; 23_anna-会员12569是学生 - 12 * 01 * 2006

表格| 03; 12_ben_会员12571是学生 - 14 * 01 * 2007



我想要的输出:

Form01 anna 12569学生12 01 2006

Form03 ben 12571学生14 01 2007



我尝试过:



StreamReader file = new StreamReader(inputfilepath);

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

while(file.EndOfStream!= true)

{

s = file.ReadLine() ;

匹配m = Regex.Match(s,(?< = student)(。)+);

if(m.Success)

{



inactive.Add(s);



}



}





使用(StreamWriter writer = new StreamWriter(outputfilepath))

{



foreach(学生中的字符串l)



{







writer.WriteLine(l);

// l = l。替换( - ,)。替换(;,)。替换(_,)。替换(|,);

I want to use several regex for my app, after reading all lines from source text file, writes only lines which contain data for the student-s.After this I want also to remove some characters and to split: (Member 12569 is student - 12*01*2006) into 5 fields as shown below:

input text file
Form|01; 23_anna- Member 12569 is student - 12*01*2006
Form|02; 17_smith_ Member 12570 is teacher - 13*01*2007
Form|03; 12_ben_ Member 12571 is student - 14*01*2007

what is printed on output file till now:
Form|01; 23_anna- Member 12569 is student - 12*01*2006
Form|03; 12_ben_ Member 12571 is student - 14*01*2007

The output I want:
Form01 anna 12569 student 12 01 2006
Form03 ben 12571 student 14 01 2007

What I have tried:

StreamReader file = new StreamReader(inputfilepath);
List<string> student = new List<string>();
while (file.EndOfStream != true)
{
s = file.ReadLine();
Match m = Regex.Match(s, "(?<=student)(.)+");
if (m.Success)
{

inactive.Add(s);

}

}


using (StreamWriter writer = new StreamWriter(outputfilepath))
{

foreach (string l in student)

{



writer.WriteLine(l);
//l = l.Replace("-", "").Replace(";", "").Replace("_", "").Replace("|", "");

推荐答案

你可以在一个正则表达式中完成:

You can do it in a single regex:
private Regex find = new Regex(@"^(.+?)(?:\|)(\d+)(?:.+?_)(.+?)(?:[_-] Member ?)(\d+)(?:.+?)(student)(?:.+?)(\d\d).(\d\d).(\d\d\d\d)


,RegexOptions.Multiline);
private void MyButton_Click( object sender,EventArgs e)
{
string sample = 表格| 01; 23_anna-会员12569是学生 - 12 * 01 * 2006 \ nForm | 02; 17_smith_会员12570是老师 - 13 * 01 * 2007 \ nForm | 03; 12_ben_会员12571是学生 - 14 * 01 * 2007;
MatchCollection matches = find.Matches(sample);
foreach (匹配m 匹配)
{
控制台。 WriteLine( {0} {1} {2} {3}为{4} {5} {6} { 7},m。Groups [ 1 ],m .Groups [ 2 ], m.Groups [ 3 ],m .Groups [ 4 ],m .Groups [ 5 ],m .Groups [ 6 ],m .Groups [ 7 ],m .Groups [ 8 ]);
}
Console.WriteLine();
", RegexOptions.Multiline); private void MyButton_Click(object sender, EventArgs e) { string sample = "Form|01; 23_anna- Member 12569 is student - 12*01*2006\nForm|02; 17_smith_ Member 12570 is teacher - 13*01*2007\nForm|03; 12_ben_ Member 12571 is student - 14*01*2007"; MatchCollection matches = find.Matches(sample); foreach (Match m in matches) { Console.WriteLine("{0}{1} {2} {3} is {4} {5} {6} {7}", m.Groups[1], m.Groups[2], m.Groups[3], m.Groups[4], m.Groups[5], m.Groups[6], m.Groups[7], m.Groups[8] ); } Console.WriteLine();


这篇关于如何将几个正则表达式用于一个方法,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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