Norbert94 Norbert94 - 1 year ago 64
Java Question

java parsing file with variable length

I have textfiles with many lines like this:

12444423100000123 123456 5147894269 123456 TEST PR/123/12/11/1/01 D222

12534223100000123 765432 5147124269 124541 PR/123/12/11/1/01 D111

I need to parse each line and create a new file. These seperate characters are not the final version. This means that I also need to split some of them that are not splitted with white space and save in different variables like:

12444423100000123 in 1244442 3100000123
String number1=1244442;
String number2=3100000123

Therefore I need to make sure to remove the white space before parsing:

String line = lines[0];
line = removeSpaces(line);

public static String removeSpaces (String arg)
Pattern whitespace = Pattern.compile("\\s");
Matcher matcher = whitespace.matcher(arg);
String result = "";
if (matcher.find()) {
result = matcher.replaceAll("");
return result;

After you remove the whitespaces the line looks like this:


I have definite lengths for each column, so I know how long each number is
therefore it is easy with the substring function, for example:

String number1= line.substring(0, 19);
String number2= line.substring(20, 26);

However, the length of these numbers (which are marked with **) are not fixed
( i dont know how long this number can be):

12444423100000123 123456 5147894269 123456 **TEST PR/123/12/11/1/01** D222

12534223100000123 765432 5147124269 124541 **PR/123/12/11/1/01** D111

However the last five last characters, which also need to be stored in a seperate column are always fixed (like D222, D111).

I dont know how to parse these numbers (PR/123/12/11/1/01 or TEST PR/123/12/11/1/01) in a sepeate column, since I dont know how long they are...

Each column is not seperated by whitespace...some columns are for example like this : 111111111222222 222233....Here I need to save 111111111, 222222 and 222233 in seperate variables, therefore I cannot use the whitespace as a seperator

Answer Source

You can use some regex, here i've printed the different groups you want you lay use them another way

public static void main(String[] args) {
   String str1 = "12444423100000123       123456 5147894269       123456        TEST PR/123/12/11/1/01 D222";
   String str2 = "12534223100000123       765432 5147124269       124541        PR/123/12/11/1/01      D111";

   String patt = "(\\d{17})\\s+(\\d{6})\\s+(\\d{10})\\s+(\\d{6})\\s+((\\s|\\w)*\\/((\\d{1,3}\\/?)*\\d{1,3}))\\s+(\\w\\d{3})"
   Pattern p = Pattern.compile(patt);

   for (String s : Arrays.asList(str1, str2) {
      Matcher m = p.matcher(s);
      if (m.find()) {                       // for str1               // str2
         System.out.println(;   // 12444423100000123       // 12534223100000123
         System.out.println(;   // 123456                  // 765432
         System.out.println(;   // 5147894269              // 5147124269
         System.out.println(;   // 123456                  // 124541
         System.out.println(;   // TEST PR/123/12/11/1/01  // PR/123/12/11/1/01
         System.out.println(;   // D222                    // D111

The pattern is only composed of capturing groups, digit groups and spaces

> Online Example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download