[英]How can I read data from specific location in .asc file using c#
我有.asc文件,其中有1000行。 行中的每一列均具有固定长度,并以一个空格分隔。 我想读取电子邮件ID列,该列从296个位置开始并连续在326个位置结束。
有什么方法可以从.asc文件中读取此类数据?
这可能会帮到您。 我只是在读取文件中的电子邮件ID,无论它是我的扩展名文件,可能是txt或asc。 同样,电子邮件地址是否位于其他位置而不是296或326也无关紧要。
public void ExtractAllEmails()
{
string datafrmAsc = File.ReadAllText(YourASCFile); //read File
Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
MatchCollection emailMatches = emailRegex.Matches(datafrmAsc);
StringBuilder sb = new StringBuilder();
foreach (Match emailMatch in emailMatches)
{
sb.AppendLine(emailMatch.Value);
}
File.WriteAllText(SomeTxtFile, sb.ToString());
}
假设这是大型文本文件,则可以执行以下操作:
List<string> emailsList = new List<string>();
int startIndex = 295;
int endIndex = 325;
using (FileStream stream = File.Open("c:\\test.asc", FileMode.Open))
using (StreamReader sr = new StreamReader(stream))
{
string line = string.Empty;
while ((line = sr.ReadLine()) != null)
{
emailsList.Add(line.Substring(startIndex, endIndex - startIndex).Trim());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.