1

正则表达式:

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);
    }
  }
4

8 回答 8

3
/Allo Allo! \(1982\) \{A Barrel Full of Airmen \(\#7\.7\)\}/
于 2010-03-02T03:31:45.870 回答
2

您可以使用split而是将其拆分在选项卡上吗?或者获取opencsv 库并使用它。

也许像

....

String[] temp;
String the_line;
BufferedReader in = new BufferedReader(new FileReader("file.txt")); 

while ((the_line = in.readLine()) != null)
{
    temp = the_line.split("\t");
    ....
}

....
于 2010-03-02T05:30:19.210 回答
1

尝试这个

        BufferedReader reader = new BufferedReader(new FileReader("yourFile"));

        Pattern p = Pattern.compile("([0-9\\.]+)[\\s]+([0-9]+)[\\s]+([0-9]\\.[0-9])[\\s]+([^\\s].*$)");

        String line;
        while( (line = reader.readLine()) != null ) {
            Matcher m = p.matcher(line);
            if ( m.matches() ) {
                 System.out.println(m.group(1));
                 System.out.println(m.group(2));
                 System.out.println(m.group(3));
                 System.out.println(m.group(4));
            }

        }

假设第三组只有一个数字 a 。然后只有一位数

于 2010-03-02T05:55:15.983 回答
1

记住编程的第一条规则:保持简单!为什么你真的需要一个正则表达式来处理整个事情?

在我看来,你有一个很好定义的表格格式......它在 tsv 中吗?

如果没有,您可以逐行读取,根据前 3 列的空格拆分,然后只有最后一列需要正则表达式来解析。

于 2010-03-02T05:29:26.170 回答
0

不,它不会。

  1. [ \t]必须变成[ \t]+or \s+; 您的数字在示例输入中使用空格(除了制表符,如果有的话)右对齐
  2. 反斜杠必须在字符串文字中进行双重转义

鉴于您希望尝试标题"'Allo 'Allo"结果Title = Allo Allo! (1982) {Lines of Communication (#7.5)}

pattern = "([0-9\\.]+)[ \\t]+([0-9]+)[ \\t]+([0-9\\.]+)[ \\t]+(.*?[ \\t]+\\([0-9]{4}\\).*)";

或(如 Fadrian 建议的那样简化):

pattern = "([\\d\\.]+)\\s+(\\d+)\\s+([\\d\\.]+)\\s+(.*?\\s+\\(\\d{4}\\).*)";

在具有该javadoc 页面名称的部分中阅读有关反斜杠、转义和引用的更多信息。Pattern

于 2010-03-02T02:02:46.203 回答
0

也许: [a-zA-Z ]+\!\(\d{4}\) \{[a-zA-Z0-9 \(\)\#\.]+\}

不确定您要完成什么,所以这是一个猜测...

为了获得更好的帮助,您必须提供更好的细节:更多示例行,这是什么类型的数据,您只是想要匹配还是想要特定的捕获组?

于 2010-03-02T03:32:53.120 回答
0

这是一个更简单的正则表达式来做你想做的事

([\d\.]*)\s*([\d\.]*)\s*([\d\.]*)\s*(.*)

如果您需要满足行尾的空白以及 \s*

([\d\.]*)\s*([\d\.]*)\s*([\d\.]*)\s*(.*)\s*

我刚刚纠正了一个使用 \S 而不是 [\d.] 的小错误

于 2010-03-02T02:40:01.923 回答
0

不要使用正则表达式来解析文本。正则表达式旨在匹配文本中的模式,而不是解析部件/组件中的文本。

如果您问题中的文本文件示例是一个实际且未更改的示例,那么以下“解析器”的基本启动示例应该可以正常工作(作为奖励,它还可以立即执行所需的 JDBC 代码)。我已将您的数据原封不动地复制粘贴到c:\test.txt.

public static void main(String... args) throws Exception {
    final String SQL = "INSERT INTO movie (distribution, votes, rank, title) VALUES (?, ?, ?, ?)";
    Connection connection = null;
    PreparedStatement statement = null;
    BufferedReader reader = null;        

    try {
        connection = database.getConnection();
        statement = connection.prepareStatement(SQL);
        reader = new BufferedReader(new InputStreamReader(new FileInputStream("/test.txt")));

        // Loop through file.
        for (String line; (line = reader.readLine()) != null;) {
            if (line.isEmpty()) continue; // I am not sure if those odd empty lines belongs in your file, else this if-check can be removed.

            // Gather data from lines.
            String distribution = line.substring(0, 10);
            int votes = Integer.parseInt(line.substring(12, 18).trim());
            double rank = Double.parseDouble(line.substring(20, 24).trim());
            String title = line.substring(26).trim().replace("\"", ""); // You also want to get rid of those double quotes, huh? I am however not sure why, maybe you initially had problems with it in your non-prepared SQL string...

            // Just to show what you've gathered.
            System.out.printf("%s, %5d, %.1f, %s%n", distribution, votes, rank, title);

            // Now add batch to statement.
            statement.setString(1, distribution);
            statement.setInt(2, votes);
            statement.setDouble(3, rank);
            statement.setString(4, title);
            statement.addBatch();
        }

        // Execute batch insert!
        statement.executeBatch();
    } finally {
        // Gently close expensive resources, you don't want to leak them!
        if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }
}

看,它只是工作。不需要过于复杂的正则表达式。

于 2010-03-02T13:10:44.517 回答