how to implement complex pattern matching in Spring batch using PatternMatchingCompositeLineMapper

2.5k views Asked by At

How can we implement pattern matching in Spring Batch, I am using org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper I got to know that I can only use ? or * here to create my pattern.

My requirement is like below: I have a fixed length record file and in each record I have two fields at 35th and 36th position which gives record type for example below "05" is record type which is at 35th and 36th position and total length of record is 400.

0000001131444444444444445589868444050MarketsABNAKKAAAAKKKA05568551456...........

I tried to write regular expression but it does not work, i got to know only two special character can be used which are * and ? .

In that case I can only write like this ??????????????????????????????????05?????????????..................

but it does not seem to be good solution.

Please suggest how can I write this solution, Thanks a lot for help in advance

1

There are 1 answers

0
Michael Minella On

The PatternMatchingCompositeLineMapper uses an instance of org.springframework.batch.support.PatternMatcher to do the matching. It's important to note that PatternMatcher does not use true regular expressions. It uses something closer to ant patterns (the code is actually lifted from AntPathMatcher in Spring Core).

That being said, you have three options:

  1. Use a pattern like you are referring to (since there is no short hand way to specify the number of ? that should be checked like there is in regular expressions).
  2. Create your own composite LineMapper implementation that uses regular expressions to do the mapping.

For the record, if you choose option 2, contributing it back would be appreciated!