我有多个字符串,格式如下:
12/18/2009 02:08:26在南大厅录入Doe,John(卡#111)[In]
从这些字符串中我需要知道该人的日期,时间,姓名和卡号。录取的词可以省略,卡号的最后一位后面的任何内容都可以忽略
我有一种感觉,我想为此使用StringTokenizer,但我并不积极。
有什么建议吗?
答案 0 :(得分:3)
当你有一个共同的分隔符时,String Tokenizer很棒,但在这种情况下我会选择正则表达式。
答案 1 :(得分:2)
我会选择java.util.Scanner ...这段代码会让你开始......你应该使用扫描仪方法的Pattern形式,而不是我使用的String形式。
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
throws Exception
{
final String str;
final Scanner scanner;
final String date;
final String time;
final String word;
final String lastName;
final String firstName;
str = "12/18/2009 02:08:26 Admitted Doe, John (Card #111) at South Lobby [In]";
scanner = new Scanner(str);
date = scanner.next("\\d+/\\d+/\\d+");
time = scanner.next("\\d+:\\d+:\\d+");
word = scanner.next();
lastName = scanner.next();
firstName = scanner.next();
System.out.println("date : " + date);
System.out.println("time : " + time);
System.out.println("word : " + word);
System.out.println("last : " + lastName);
System.out.println("first: " + firstName);
}
}
答案 2 :(得分:2)
您的记录格式非常简单,我只需使用String的split方法来获取日期和时间。正如评论中所指出的那样,拥有可以包含空格的名称会使事情变得复杂,以至于按空格分割记录将不适用于每个字段。我使用正则表达式来获取其他三条信息。
public static void main(String[] args) {
String record1 = "12/18/2009 02:08:26 Admitted Doe, John (Card #111) at South Lobby [In]";
String record2 = "12/18/2009 02:08:26 Admitted Van Halen, Eddie (Card #222) at South Lobby [In]";
String record3 = "12/18/2009 02:08:26 Admitted Thoreau, Henry David (Card #333) at South Lobby [In]";
summary(record1);
summary(record2);
summary(record3);
}
public static void summary(String record) {
String[] tokens = record.split(" ");
String date = tokens[0];
String time = tokens[1];
String regEx = "Admitted (.*), (.*) \\(Card #(.*)\\)";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(record);
matcher.find();
String lastName = matcher.group(1);
String firstName = matcher.group(2);
String cardNumber = matcher.group(3);
System.out.println("\nDate: " + date);
System.out.println("Time: " + time);
System.out.println("First Name: " + firstName);
System.out.println("Last Name: " + lastName);
System.out.println("Card Number: " + cardNumber);
}
正则表达式"Admitted (.*), (.*) \\(Card #(.*)\\)"
使用分组括号来存储您尝试提取的信息。必须转义记录中存在的括号。
运行上面的代码给出了以下输出:
Date: 12/18/2009
Time: 02:08:26
First Name: John
Last Name: Doe
Card Number: 111
Date: 12/18/2009
Time: 02:08:26
First Name: Eddie
Last Name: Van Halen
Card Number: 222
Date: 12/18/2009
Time: 02:08:26
First Name: Henry David
Last Name: Thoreau
Card Number: 333
答案 3 :(得分:1)
在解析此行时要记住以下几点:
由于这个原因,我会使用TofuBeer的答案并调整下一个名字。由于额外的空间,字符串拆分会变得混乱。
答案 4 :(得分:0)
最短的正则表达式解决方案(使用类型转换):
String stringToParse = "12/18/2009 02:08:26 Admitted Doe, John (Card #111) at South Lobby [In] ";
Pattern pattern = Pattern.compile("((\\d{2}/){2}\\d{4}\\s(\\d{2}:){2}\\d{2})\\s(\\w+)\\s((.*)),\\s((.*))\\s.*#(\\d+)");
Matcher matcher = pattern.matcher(stringToParse);
matcher.find();
String firstName = matcher.group(6);
String lastName = matcher.group(5);
int cardNumber = Integer.parseInt(matcher.group(7));
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = df.parse(matcher.group(1));
答案 5 :(得分:-1)
相信你的勇气...... :) 使用StringTokenizer:
import java.io.*; import java.util.StringTokenizer; public class Test { public Test() { }
public void execute(String str) { String date, time, firstName, lastName, cardNo; StringTokenizer st = new StringTokenizer(str, " "); date = st.nextToken(); time = st.nextToken(); st.nextToken(); //Admitted lastName = st.nextToken(",").trim(); firstName = st.nextToken(",(").trim(); st.nextToken("#"); //Card cardNo = st.nextToken(")#"); System.out.println("date = " + date +"\ntime = " + time +"\nfirstName = " + firstName +"\nlastName = "+ lastName +"\ncardNo = " +cardNo); }
public static void main(String args[]) { Test t = new Test(); String record1 = "12/18/2009 02:08:26 Admitted Doe, John (Card #111) at South Lobby [In]"; String record2 = "12/18/2009 02:08:26 Admitted Van Halen, Eddie (Card #222) at South Lobby [In]"; String record3 = "12/18/2009 02:08:26 Admitted Thoreau, Henry David (Card #333) at South Lobby [In]"; t.execute(record1); t.execute(record2); t.execute(record3); } }