Regex:
String regexp = "([0-9.]{1,15})[ \t]*([0-9]{1,15})[ \t]*([0-9.]{1,15})[ \t]*(\"(.*?)\"\\s+\\((\\d{4})\\)\\s+\\{(.*?)\\})";
Text:
1000000103 50 4.5 #1 Single (2006) 2...1.2.12 8 2.7 $1,000,000 Chance of a Lifetime (1986) 11..2.2..2 8 5.0 $100 Taxi Ride (2001) ....13.311 9 7.1 $100,000 Name That Tune (1984) 3..21...22 10 4.6 $2 Bill (2002) 30010....3 18 2.7 $25 Million Dollar Hoax (2004) 2000010002 111 5.6 $40 a Day (2002) 2000000..4 26 1.6 $5 Cover (2009) .0..2.0122 15 7.8 $9.99 (2003) ..2...1113 8 7.5 $weepstake$ (1979) 0000000125 3238 8.7 Allo Allo! (1982) 1....22.12 8 6.5 Allo Allo! (1982) {A Barrel Full of Airmen (#7.7)
I'm trying to use Java and MySQL together. I'm learning it for a project that I'm planning. I want the desired output to be like this:
distribution = first column
rank = second column
votes = thirst column
title = fourth column
The first three work fine. I have trouble with the fourth one.
no well there are suppose to be curly brackets this is like the first few entries ill paste a few more it may make it easier to realize what i'm trying to show you. So here they are:
0...001122 16 7.8 "'Allo 'Allo!" (1982) {Gruber Does Some Mincing (#3.2)} 100..01103 21 7.4 "'Allo 'Allo!" (1982) {Hans Goes Over the Top (#4.1)} ....022100 11 6.9 "'Allo 'Allo!" (1982) {Hello Hans (#7.4)} 0....03022 21 8.4 "'Allo 'Allo!" (1982) {Herr Flick's Revenge (#2.6)} ......8..1 6 7.0 "'Allo 'Allo!" (1982) {Hitler's Last Heil (#8.3)} .....442.. 5 6.5 "'Allo 'Allo!" (1982) {Intelligence Officers (#6.5)} ....1123.2 9 6.9 "'Allo 'Allo!" (1982) {It's Raining Italians (#6.2)} ....1.33.3 10 7.8 "'Allo 'Allo!" (1982) {Leclerc Against the Wall (#5.18)} ....22211. 8 6.4 "'Allo 'Allo!" (1982) {Lines of Communication (#7.5)}
The code i'm using:
stmt.executeUpdate("CREATE TABLE mytable(distribution char(20)," +
"votes integer," + "rank float," + "title char(250));");
String regexp ="([\\d\\.]+)\\s+(\\d+)\\s+([\\d\\.]+)\\s+(.*?\\s+\\(\\d{4}\\).*)";
Pattern pattern = Pattern.compile(regexp);
String line;
String data= "";
while ((line = bf.readLine()) != null) {
data = line.replaceAll("'", " ");
String data2 = data.replaceAll("\"", "");
//System.out.println(data2);
Matcher matcher = pattern.matcher(data2);
if (matcher.find()) {
String distribution = matcher.group(1);
String votes = matcher.group(2);
String rank = matcher.group(3);
String title = matcher.group(4);
//System.out.println(distribution + " " + votes + " " + rank + " " + title);
String todo = ("INSERT into mytable " +
"(Distribution, Votes, Rank, Title) "+
"values ('"+distribution+"', '"+votes+"', '"+rank+"', '"+title+"')");
stmt = con.createStatement();
int r = stmt.executeUpdate(todo);
}
}