正则表达式:
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 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)
我正在尝试一起使用Java和MySQL。我正在为一个我正在计划的项目学习它。我希望所需的输出是这样的:
distribution = first column
rank = second column
votes = thirst column
title = fourth column
前三个工作正常。我遇到了第四个问题。
不好有假设是大括号这就像前几个条目生病粘贴一些它可能会让我更容易实现我想告诉你的东西。所以这里是:
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)}
我正在使用的代码:
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);
}
}
答案 0 :(得分:3)
/Allo Allo! \(1982\) \{A Barrel Full of Airmen \(\#7\.7\)\}/
答案 1 :(得分:2)
您可以使用split而不是在标签上拆分吗?或者获取opencsv library并使用它。
也许像
....
String[] temp;
String the_line;
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
while ((the_line = in.readLine()) != null)
{
temp = the_line.split("\t");
....
}
....
答案 2 :(得分:1)
记住编程的第一条规则:保持简单! 为什么你真的需要整个正则表达式?
在我看来,你有一个很好定义的表格格式......是在tsv吗?
如果没有,你可以逐行阅读,根据前3列的空格进行拆分,然后只需要你的最后一列需要正则表达式进行解析。
答案 3 :(得分: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。然后只有一位数
答案 4 :(得分:0)
不,不会。
[ \t]
必须成为[ \t]+
或\s+
;您的数字在样本输入中使用空格(除了制表符,如果有的话)右对齐鉴于您希望"'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}\\).*)";
在Pattern
javadoc页面的名称部分中详细了解Backslashes, escapes, and quoting。
答案 5 :(得分:0)
这是一个更简单的正则表达式来做你想做的事情
([\d\.]*)\s*([\d\.]*)\s*([\d\.]*)\s*(.*)
如果您需要在行尾添加空格,那么就需要\ s *
([\d\.]*)\s*([\d\.]*)\s*([\d\.]*)\s*(.*)\s*
我刚刚纠正了使用\ S而不是[\ d。]
的小错误答案 6 :(得分:0)
也许:
[a-zA-Z ]+\!\(\d{4}\) \{[a-zA-Z0-9 \(\)\#\.]+\}
不确定你想要完成什么,所以这是一种猜测......
为了获得更好的帮助,您必须提供更好的详细信息:更多示例行,这是什么类型的数据,您只是想要匹配还是您想要特定的捕获组?
答案 7 :(得分: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) {}
}
}
看,它只是有效。不需要过于复杂的正则表达式。