我是MT940文件的新手,我正在搜索一个示例代码来读取.mt940的内容,并将其存储在一个数据库表中,其中包含相应的字段。我在努力分析它。有任何简单的方法来解析并保存在表中吗?
例如,以下面的行为例(这不是完整的mt940,只是一行)
:61:2009230923D4086,74 NONREF //NONREF
如何从上面的行检索客户引用(=NONREF)?它并不存在于所有行的相同索引中。有的从28指数开始,有的从30指数开始。
发布于 2021-09-25 15:39:02
使用Regex
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApp1
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
const string pattern = @":61:(?'ValueDate'.{6})(?'EntryDate'.{4})(?'Mark'.{2})(?'FundCode'.{1})(?'Amount'[\d,]+)(?'ID'.{4})((?'CustomerReference'.{1,16})(//)(?'BankReference'.*)|(?'CustomerReference'.{1,16}))";
static void Main(string[] args)
{
string data = File.ReadAllText(FILENAME);
MatchCollection matches = Regex.Matches(data, pattern, RegexOptions.Multiline);
foreach (Match match in matches)
{
string valueDate = match.Groups["ValueDate"].Value;
string entryDate = match.Groups["EntryDate"].Value;
string mark = match.Groups["Mark"].Value;
string fundCode = match.Groups["FundCode"].Value;
string amount = match.Groups["Amount"].Value;
string id = match.Groups["ID"].Value;
string customerReference = match.Groups["CustomerReference"].Value;
string bankReference = match.Groups["BankReference"].Value;
Console.WriteLine("ValueDate = {0}, EntryDate = {1}, Mark = {2}, FundCode = {3}, Amount = {4}, ID = {5}, Customer = {6}, Bank = {7}",
valueDate, entryDate, mark, fundCode, amount, id, customerReference, bankReference);
}
Console.ReadLine();
}
}
}https://stackoverflow.com/questions/69303626
复制相似问题