Simple solution: ".{6}22.{5}\\s+.{6}33.{5}"
. Note that \s+
is a shorthand for consequent whitespace elements.
Heres an example:
public static void main(String[] argv) throws FileNotFoundException {
String input = "yXX00002200000\r\nXX00003300000\nshort", regex = ".{6}22.{5}\\s+.{6}33.{5}", result = "";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(input);
while (m.find()) {
result = m.group();
System.out.println(result);
}
}
With output:
XX00002200000
XX00003300000
To play around with Java Regex you can use: Regular Expression Editor (free online editor)
Edit: I think that you are changing the input when you are reading data, try:
public static String readFile(String filename) throws FileNotFoundException {
Scanner sc = new Scanner(new File(filename));
StringBuilder sb = new StringBuilder();
while (sc.hasNextLine())
sb.append(sc.nextLine());
sc.close();
return sb.toString();
}
Or
static String readFile(String path) {
FileInputStream stream = null;
FileChannel channel = null;
MappedByteBuffer buffer = null;
try {
stream = new FileInputStream(new File(path));
channel = stream.getChannel();
buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0,
channel.size());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
stream.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return Charset.defaultCharset().decode(buffer).toString();
}
With imports like:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;