정규식 :이 텍스트 줄에이 정규 표현식을 사용할 수 있습니까?
String regexp = "([0-9.]{1,15})[ \t]*([0-9]{1,15})[ \t]*([0-9.]{1,15})[ \t]*(\"(.*?)\"\\s+\\((\\d{4})\\)\\s+\\{(.*?)\\})";
텍스트 : 내가 함께 자바와 MySQL을 사용하려고 해요
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)
. 나는 내가 계획하고있는 프로젝트를 위해 그것을 배우고있다. 원하는 출력을 다음과 같이 지정하십시오.
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);
}
}
아마도 "work"는 "예제 입력의 각 줄마다 원하는 부분 문자열을 캡처합니다"를 의미합니다.이 부분은 테스트 할 수 있습니다. 특히 원하는 결과가 무엇인지 확실하지 않기 때문에 특히 그렇습니다. 숙제 일 수는 없지, 그렇지? – Cascabel
"Allo 'Allo"제목 검색 결과는 이어야합니다. 제목 = Allo Allo! (1982) {Lines of Communication (# 7.5)} –
무엇을 찾으려고합니까? – jasonbar