更新时间:2023-08-29 09:29:28
没有必要加载整个文件先存入内存。您可以逐行处理:使用 System.IO;
使用 System.Text.RegularExpressions;
命名空间 ConsoleApplication17
{
class Program
{
私有 静态正则表达式= new 正则表达式( @ (PR \ * * -\ * * [A-Za] z0-9 - ] +),RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
静态 void Main( string [] args)
{
string myFile = @ .. \..\TextFile1.txt跨度>;
string writeFile = @ .. \\ \\..\TextFile1.csv跨度>;
使用(TextWriter writer = new StreamWriter(writeFile))
{
foreach (字符串行 文件.ReadLines(myFile))
{
匹配m = Pattern.Match(line);
if (m.Success)
{
writer.Write(m.Groups [ 1 跨度>]值)。
writer.Write( ,);
writer.WriteLine(line);
}
}
}
}
}
}
你可以尝试这样的事情:string input = File.ReadAllText(myfile);
正则表达式正则表达式= 新正则表达式( @ ^(小于COL2> *(小于COL1>??PR\s * -\s * [A-ZA-Z0-9 - ] +)*)
,RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline);
MatchCollection matchCollection = regex.Matches(input);
使用(TextWriter tw = new StreamWriter(writeFile))
{
foreach (在 matchCollection中匹配m )
{
string col1 = m.Groups [ col1]。值; // 仅提取的部分
string col2 = m.Groups [ col2]。 // 完整行
tw.WriteLine( {0},{1},col1,col2);
}
}
结果PR-EG4090-28,\ \192.168.8.212\c\Avago.ATF.Common\Clotho_SDI_Data\FBAR_2MS5-1505_N3131-3033_PR-EG4090-28_FULL-1PASS_CHAN1_20160610_104333_IP192.168.0.2_G6AABRH
var myfile = (@"C:\Log\all.txt");
string[] lin = File.ReadAllLines(myfile);
string pat01 = @"PR -([A-Za-z0-9-]+)";
MatchCollection matches = Regex.Matches((lin[1]), pat01);
foreach (Match match in matches)
{
string writeFile = @"E:\DataLocate_PR_id.csv";
string readFile = @"C:\Log\all.txt";
StringBuilder csv = new StringBuilder();
csv.AppendLine(match + readFile);
File.AppendAllText(writeFile, csv.ToString());
}
What I have tried:
This is my 1s try and many of code hard to understand that I read from other forums.
It isn't necessary to load the whole file into memory first. You can process it line-by-line:using System.IO; using System.Text.RegularExpressions; namespace ConsoleApplication17 { class Program { private static Regex Pattern = new Regex(@"(PR\s*-\s*[A-Za-z0-9-]+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); static void Main(string[] args) { string myFile = @"..\..\TextFile1.txt"; string writeFile = @"..\..\TextFile1.csv"; using (TextWriter writer = new StreamWriter(writeFile)) { foreach (string line in File.ReadLines(myFile)) { Match m = Pattern.Match(line); if (m.Success) { writer.Write(m.Groups[1].Value); writer.Write(","); writer.WriteLine(line); } } } } } }
You can try something like this:string input = File.ReadAllText(myfile); Regex regex = new Regex(@"^(?<col2>.*(?<col1>PR\s*-\s*[A-Za-z0-9-]+).*)
", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline); MatchCollection matchCollection = regex.Matches(input); using (TextWriter tw = new StreamWriter(writeFile)) { foreach (Match m in matchCollection) { string col1 = m.Groups["col1"].Value; // Only the extracted part string col2 = m.Groups["col2"].Value; // The full line tw.WriteLine("{0},{1}", col1, col2); } }
ResultPR-EG4090-28,\\192.168.8.212\c\Avago.ATF.Common\Clotho_SDI_Data\FBAR_2MS5-1505_N3131-3033_PR-EG4090-28_FULL-1PASS_CHAN1_20160610_104333_IP192.168.0.2_G6AABRH