I inherited a bank interface parser. The previous developer actually did this pretty slick. The file that comes in from the bank is a fixed length field. The way he parses that record from the download is this
public static final String HEADER_RECORD_REGEX = "^(\\d{3})(\\d{12})(.{20})(\\d\\d)(\\d\\d)(\\d\\d)(\\d{12})(\\d\\d)$";
private static final int BANK_ID = 1;
private static final int ACCOUNT_ID = 2;
private static final int COMPANY_NAME = 3;
private static final int MONTH = 4;
private static final int DAY = 5;
private static final int YEAR = 6;
private static final int SEQUENCE = 7;
private static final int TYPE_CODE = 8;
private static final int GROUP_COUNT = TYPE_CODE;
if ( GROUP_COUNT == matcher.groupCount() ) {
setBankId( matcher.group( BANK_ID ) );
setAccountId( matcher.group( ACCOUNT_ID ) );
setCompanyName( matcher.group( COMPANY_NAME ) );
setProcessDate( matcher.group( MONTH ), matcher.group( DAY ),
matcher.group( YEAR ) );
setSeqNumber( matcher.group( SEQUENCE ) );
setTypeCode( matcher.group( TYPE_CODE ) );
}
I have a new requirement to reverse this process and actually generate mock files from the bank so we can test. Using this method, is there a way i can reverse the process using this same regex method to generate the file or do i just go back to building a standard parser.
thanks
new Formatter().format("%3.3s%12.12s%20.20s%2.2s%2.2s%2.2s%12.12s%2.2s", bankID, acctID, companyName, month, day, year, seq, typeCode);or something similar. Also, in retrospect, it would have been much more useful for our clever parser writer to programmatically define the lengths of each field instead of hardcoding them into his regex.GROUP_COUNTdoesn't make sense. The value returned bymatcher.groupCount()is a static property of the Pattern object associated with the Matcher. It will always be the same, even if the match attempt fails.