Skip to content

Instantly share code, notes, and snippets.

@luan-cestari
Created September 8, 2013 05:09
Show Gist options
  • Save luan-cestari/6482016 to your computer and use it in GitHub Desktop.
Save luan-cestari/6482016 to your computer and use it in GitHub Desktop.
Processing the file to extract data using REGEX
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws Exception{
// The Regex to extract the data from the input. It is using named captured group from JDK7 but it can be removed (just remove the <name> in capturing group in bellow the pattern and use matcher.group(x) instead of the name of the group)
Pattern pattern =
Pattern.compile("--MIME_boundary-\\d*\\n(?:Content-Type: text/xml; charset=\"UTF-8\"\\nContent-Transfer-Encoding: 8bit\\nContent-Location: dataHeader\\.xml\\n<\\?xml version=\"1\\.0\" encoding=\"UTF-8\"\\?>\\n<Header>\\n <DataLen>(?<first>.*)</DataLen>|Content-Type: application/octet-stream\\nContent-Location: data\\.bin\\n(?<second>.*)\\n)");
// read the intere file named "input" into a String instance
Matcher matcher = pattern.matcher(Charset.defaultCharset().decode(ByteBuffer.wrap(Files.readAllBytes(Paths.get("input")))).toString());
boolean found = false;
while (matcher.find()) {
System.out.println(matcher.group("first")); //matcher.group(1)
System.out.println(matcher.group("second")); //if matcher.group(1) is null, matcher.group(2)
System.out.println("!!!!!!!!!!!!");
found = true;
}
if(!found){
System.out.println("No match found");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment