且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将正则表达式匹配到文本文件的问题

更新时间:2022-10-14 22:17:13

你确实意识到Regex.Split功能的作用,不要''你呢?它提取匹配的数据并拆分输入字符串的其余部分?

所以

  string  input =   hello; there; paul; 
File.WriteAllLines( @ D:\Temp \op.txt, Regex.Split(输入, ;);

会生成一个文件完全没有分号?

你好

paul

我怀疑你的描述中你想要提取电子邮件和电话号码:所以使用您之前创建的MatchCollections,几个 foreach 循环并从那里写入文本...


你应该尝试一些工具来设计你的正则表达式:

Expresso - 用于构建和测试正则表达式的工具 [ ^ ]



编辑:

你应该阅读e关于此主题的Microsoft文档:

http://msdn.microsoft.com /en-us/library/s2tte0y1.aspx [ ^ ]

http://msdn.microsoft。 com / en-us / library / 3det53xh.aspx [ ^ ]

hi all,
in my form by clicking a button i read a text file that have email address and telephone number in it.by using regex class and a pattern i can know count of matches with my pattern.but i do not can write this matches to another text file.please help?

You do realize what the Regex.Split function does, don''t you? That it extracts the matched data and splits the rest of the input string?
So
string input = "hello;there;paul";
File.WriteAllLines(@"D:\Temp\op.txt", Regex.Split(input, ";");

Would produce a file completely lacking in semicolons?

hello
there
paul

I suspect from your description that you want to extract the emails and phone numbers: so use the MatchCollections you created earlier, a couple of foreach loops and write the text from there...


You shoulod try some tools to design your regex:
Expresso - A Tool for Building and Testing Regular Expressions[^]

Edit:
You should read the Microsoft documentation on the subject:
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx[^]
http://msdn.microsoft.com/en-us/library/3det53xh.aspx[^]