0

I am new to MT940 file and I am searching for a sample code to read content of .mt940 and store in a database table with respective fields. I am struggling to analyze it. Is there any simple way to parse and save it in a table?

For example take the below line(this is not entire mt940 just a single line)

:61:2009230923D4086,74NDDTNONREF //NONREF

How can I retrieve customer reference(=NONREF ) from the above line? It does not present in same index for all lines. Some times it starts from 28index and some times 30 index.

Bin
  • 59
  • 5
  • What is issue. See following : https://www.sepaforcorporates.com/swift-for-corporates/account-statement-mt940-file-format-overview/?force_isolation=true – jdweng Sep 23 '21 at 16:52
  • See also : https://www.sepaforcorporates.com/swift-for-corporates/read-swift-message-structure/?force_isolation=true – jdweng Sep 23 '21 at 16:59
  • @jdweng I have updated the question explaining where I am facing issue. Kindly take a look. – Bin Sep 24 '21 at 05:49
  • You have to parse into field one field at a time. The width of each field may be different sizes. For example you may have the number 123 or a number 123.45 – jdweng Sep 24 '21 at 19:53
  • That is what, how do we know that the field starts here and ends here to get substring? – Bin Sep 25 '21 at 03:01
  • The thing is I don't need to read entire file. I need to parse only :61: tags. I am using vb.net string functions to get the values. But I am confused about index values to use substring operation. Kindly help. – Bin Sep 25 '21 at 08:54

1 Answers1

0

Use 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();

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • How to make 'Mark' and 'FundCode' fields optional? Sometimes I don't have data in those 2 fields. – Bin Sep 26 '21 at 04:16
  • The field are optional and will work with or without the field. I updated code to get all the 61 fields using a Match Collection – jdweng Sep 27 '21 at 00:40
  • I am getting amount as 86,74 but the correct answer should be 4086,74. That means mark and fund code is picking some of amounts digits – Bin Sep 27 '21 at 05:33
  • I was using the 2nd link that I posted above which says following : 6!n[4!n]2a[1!a]15d1!a3!c16x[//16x] The number should be 86,74. – jdweng Sep 27 '21 at 10:21