起因是因爲一片帖子,問到了一個問題,帖子是這樣的:
我想編一個程序,但學CompSci是很久以前的事情了。想請教請教大家。
有兩個txt文件,一個叫source.txt(有很多數據), 一個叫target.txt(空白的)
我想把source.txt裏的一些數據提取出來(稍微修改一下),然後寫到target.txt裏面。
舉個例子:
sourse.txt裏的數據:
2oi)4@##( "data:001%abc">dsi-23)(*32##("data:dce%xy3"#(*EOIj2308Eld
想提取的數據就是橘黃色的。
data:001%abc
全部提取出來以後,我還想把%換成*, 然後每條數據後面加個逗號","
最後target.txt就應該這樣:
data:001*abc,
data:dce*xyz
請問應該怎麼做啊?實在JAVA忘光了。求教~~
如果幫我做的話付點酬勞也可以。
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace RegExpression
{
/// <summary>
///
/// </summary>
public class DataFilter
{
public static void Main(string[] args)
{
if( args.Length < 2 )
{
Console.Error.WriteLine("Please enter 2 filenames(e.g. In.txt Out.txt)");
return;
}
string Result;
using( StreamReader sr = new StreamReader(args[0]) )
{
Result = Filter( sr.ReadToEnd() );
}
using( StreamWriter wr = new StreamWriter(args[1]) )
{
wr.Write(Result);
}
}
private static string Filter(string input)
{
StringBuilder result = new StringBuilder();
Regex r = new Regex("/"(?<data>//w+):(?<key>//w+)%(?<value>//w+)/"", RegexOptions.Compiled);
for( Match m = r.Match(input); m.Success; m = m.NextMatch() )
{
result.Append( m.Result("${data}:${key}*${value},"+Environment.NewLine) );
}
return result.ToString();
}
}
}