正则表达式:
String regexp = "([0-9.]{1,15})[ \t]*([0-9]{1,15})[ \t]*([0-9.]{1,15})[ \t]*(\"(.*?)\"\\s+\\((\\d{4})\\)\\s+\\{(.*?)\\})";
文本:
1000000103 50 4.5 #1 单曲 (2006) 2...1.2.12 8 2.7 $1,000,000 一生的机会 (1986) 11..2.2..2 8 5.0 $100 出租车 (2001) ....13.311 9 7.1 $100,000 命名那曲子 (1984) 3..21...22 10 4.6 2 美元钞票 (2002) 30010....3 18 2.7 2500 万美元骗局 (2004) 2000010002 111 5.6 每天 40 美元 (2002) 2000000..4 26 1.6 $5 封面 (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 喂喂喂!(1982){装满飞行员的桶(#7.7)
我正在尝试同时使用 Java 和 MySQL。我正在为我正在计划的项目学习它。我希望所需的输出是这样的:
distribution = first column
rank = second column
votes = thirst column
title = fourth column
前三个工作正常。我对第四个有问题。
不好,假设有大括号,这就像前几个条目一样粘贴更多,这可能会让我更容易意识到我想向你展示的内容。所以他们在这里:
0...001122 16 7.8 "'Allo'Allo!" (1982){格鲁伯做了一些切碎(#3.2)} 100..01103 21 7.4 “阿洛阿洛阿洛!” (1982 年){汉斯走上巅峰(#4.1)} ....022100 11 6.9 “'Allo'Allo!” (1982) {你好汉斯 (#7.4)} 0....03022 21 8.4 "'Allo'Allo!" (1982) {弗利克先生的复仇 (#2.6)} ......8..1 6 7.0 "'Allo 'Allo!" (1982 年){希特勒的最后一战(#8.3)} .....442.. 5 6.5 "'Allo'Allo!" (1982) {情报官员 (#6.5)} ....1123.2 9 6.9 “'Allo'Allo!” (1982) {意大利下雨了 (#6.2)} ....1.33.3 10 7.8 “'Allo'Allo!” (1982) {勒克莱尔靠墙 (#5.18)} ....22211。8 6.4 “阿洛阿洛!” (1982) {通信线路 (#7.5)}
我正在使用的代码:
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);
}
}