Skip to content

Instantly share code, notes, and snippets.

@superboum
Created April 23, 2016 15:39
Show Gist options
  • Save superboum/d2807207732d17f8e7ffc507b0de3dd8 to your computer and use it in GitHub Desktop.
Save superboum/d2807207732d17f8e7ffc507b0de3dd8 to your computer and use it in GitHub Desktop.
Regex with Java (named Pattern)
import java.io.*;
import java.util.regex.*;
public class Reader {
public static void main(String[] args) {
try {
String[] cols = {"pion", "x", "y", "joueur", "jouable"};
String filename = "standard.txt";
String line = null;
Pattern p = Pattern.compile("-?\\d+");
int i;
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
Matcher m = p.matcher(line);
i = 0;
while (m.find()) {
System.out.print(cols[i++]+" -> "+m.group()+"\t");
}
System.out.println("");
}
bufferedReader.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
03 1 0 1 1
3 8 0 1 1
3 1 9 2 1
3 8 9 2 1
2 2 0 1 1
2 7 0 1 1
2 1 1 1 1
2 8 1 1 1
2 2 9 2 1
2 7 9 2 1
2 1 8 2 1
2 8 8 2 1
2 1 8 2 1
1 3 0 1 1
1 6 0 1 1
1 2 1 1 1
1 7 1 1 1
1 1 2 1 1
1 7 1 1 1
1 8 2 1 1
1 3 9 2 1
1 6 9 2 1
1 2 8 2 1
1 7 8 2 1
1 1 7 2 1
1 8 7 2 1
-2 4 1 1 -2
-2 5 1 1 -2
-2 4 8 2 -2
-2 5 8 2 -2
pion -> 03 x -> 1 y -> 0 joueur -> 1 jouable -> 1
pion -> 3 x -> 8 y -> 0 joueur -> 1 jouable -> 1
pion -> 3 x -> 1 y -> 9 joueur -> 2 jouable -> 1
pion -> 3 x -> 8 y -> 9 joueur -> 2 jouable -> 1
pion -> 2 x -> 2 y -> 0 joueur -> 1 jouable -> 1
pion -> 2 x -> 7 y -> 0 joueur -> 1 jouable -> 1
pion -> 2 x -> 1 y -> 1 joueur -> 1 jouable -> 1
pion -> 2 x -> 8 y -> 1 joueur -> 1 jouable -> 1
pion -> 2 x -> 2 y -> 9 joueur -> 2 jouable -> 1
pion -> 2 x -> 7 y -> 9 joueur -> 2 jouable -> 1
pion -> 2 x -> 1 y -> 8 joueur -> 2 jouable -> 1
pion -> 2 x -> 8 y -> 8 joueur -> 2 jouable -> 1
pion -> 2 x -> 1 y -> 8 joueur -> 2 jouable -> 1
pion -> 1 x -> 3 y -> 0 joueur -> 1 jouable -> 1
pion -> 1 x -> 6 y -> 0 joueur -> 1 jouable -> 1
pion -> 1 x -> 2 y -> 1 joueur -> 1 jouable -> 1
pion -> 1 x -> 7 y -> 1 joueur -> 1 jouable -> 1
pion -> 1 x -> 1 y -> 2 joueur -> 1 jouable -> 1
pion -> 1 x -> 7 y -> 1 joueur -> 1 jouable -> 1
pion -> 1 x -> 8 y -> 2 joueur -> 1 jouable -> 1
pion -> 1 x -> 3 y -> 9 joueur -> 2 jouable -> 1
pion -> 1 x -> 6 y -> 9 joueur -> 2 jouable -> 1
pion -> 1 x -> 2 y -> 8 joueur -> 2 jouable -> 1
pion -> 1 x -> 7 y -> 8 joueur -> 2 jouable -> 1
pion -> 1 x -> 1 y -> 7 joueur -> 2 jouable -> 1
pion -> 1 x -> 8 y -> 7 joueur -> 2 jouable -> 1
pion -> -2 x -> 4 y -> 1 joueur -> 1 jouable -> -2
pion -> -2 x -> 5 y -> 1 joueur -> 1 jouable -> -2
pion -> -2 x -> 4 y -> 8 joueur -> 2 jouable -> -2
pion -> -2 x -> 5 y -> 8 joueur -> 2 jouable -> -2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment