Charles
Charles

Reputation: 112

Extract value from ISO message string

I have this string from the Field 54 of an ISO message:

1002566C0000000000001004566C0000000000001001566C000000000000 

It is divided into sections/data elements as shown below based on the definition from the PostBridge Interface Specification:

Information on up to 6 amounts and related account data for which specific data elements have not been defined. Each amount is a fixed length field consisting of 5 data elements:

  • Account type (positions 1 - 2)
  • Amount type (positions 3 - 4)
  • Currency code (positions 5 - 7)
  • Amount sign (position 8) - "C" or "D"
  • Amount (position 9 - 20)
10 
02
566
C
000000000000

10
04
566
C
000000000000

10
01
566
C
000000000000

Is there a simple way to parse this String with JPos using something like the TLVList or some other class? Or do I have to create a custom implementation to parse this value myself.

Upvotes: 1

Views: 362

Answers (1)

WJS
WJS

Reputation: 40034

Here is one way to do it. A simple regular expression and a record (or a class).

record Info(String getAccount, String getAmountType, String getCurrencyCode,
        String getSign, String getAmount){
        @Override
        public String toString() {
            return """ 
                    Account:       %s
                    AmountType:    %s
                    Currency Code: %s
                    AmountSign:    %s
                    Amount:        %s
                    """.formatted(getAccount, getAmountType, getCurrencyCode,getSign,getAmount);
        }       
    }

String s = "1002566C0000000000001004566C0000000000001001566C000000000000";
String regex = "String regex = "(\\d\\d)(\\d\\d)(\\d{3})(C|D)(\\d{12})";


List<Info> accts = new ArrayList<>();
Matcher m = Pattern.compile(regex).matcher(s);
while (m.find()) {
    accts.add(new Info(m.group(1),m.group(2), m.group(3),m.group(4),m.group(5)));
}

for (Info info : accts) {
    System.out.println(info);
}

prints

Account:       10
AmountType:    02
Currency Code: 566
AmountSign:    C
Amount:        000000000000

Account:       10
AmountType:    04
Currency Code: 566
AmountSign:    C
Amount:        000000000000

Account:       10
AmountType:    01
Currency Code: 566
AmountSign:    C
Amount:        000000000000

Any fine tuning such as tailoring the record types or toString should be trivial. It does assume the record is well formed in term of a consistent number of characters per record. Else the regex would fail for missing groups.

Upvotes: 1

Related Questions