我有这个文字示例:
1. Luhn, H.P.:
2. Steinberger, J., Kian, M.:
3. Murray, G., Renals, S., Carletta J.:
4. Yeh, J.Y., Ke, H.R., Yang, W.P, Meng, I.H.:
. . .
32. McKeown, K., Klavans, J., Hatzivassiloglou, V., Barzilay, R., Eskin, E.:
对于每一行,我想在string[] y
中获取并保存名字。例如,y
数组的内容应为:
y[0]="Luhn"
y[1]="Steinberger and Kian"
y[2]="Murray, Renals and Carletta"
y[3]="Yeh et al "
...
y[i-1]="McKeown et al"
我尝试将split(String regex, int limit)
与
split("[A-Z][a-z],[A-Z][a-z],[A-Z][a-z]:", 3)
但是正则表达式是错误的,我不明白limit
参数是什么意思。
答案 0 :(得分:1)
如果我理解得很好,您需要转换引用列表,但实际上您需要能够减少一个引用,例如"Murray, G., Renals, S., Carletta, J.:"
喜欢的东西
“穆雷,雷纳斯和卡莱塔”
好吧,一个简单的Java程序就可以帮助您入门。其目的只是为了展示如何进行必要的转型:
public class Citations {
private static String process (String line) {
String[] words = line.split(",");
List<String> surnames = new ArrayList<>();
if (words.length == 0) {
return "";
}
for (int i = 0; i < words.length; i += 2) {
surnames.add(words[i].trim());
}
if (surnames.size() == 1) {
return surnames.get(0);
} else if (surnames.size() == 2) {
return surnames.get(0) + " and " + surnames.get(1);
} else if (surnames.size() == 3) {
return surnames.get(0) + ", " + surnames.get(1) + " and " + surnames.get(2);
} else {
return surnames.get(0) + " et al";
}
}
public static void main(String[] args) {
String[] lines = { "Luhn, H.P.:",
"Steinberger, J., Kian, M.:",
"Murray, G., Renals, S., Carletta, J.:",
"Yeh, J.Y., Ke, H.R., Yang, W.P, Meng, I.H.:"
};
for (String line : lines) {
String s = line + " -> " + process(line);
System.out.println(s);
}
}
}
我发现它可以满足您的需求,因为我在运行它时会得到它:
Luhn, H.P.: -> Luhn
Steinberger, J., Kian, M.: -> Steinberger and Kian
Murray, G., Renals, S., Carletta, J.: -> Murray, Renals and Carletta
Yeh, J.Y., Ke, H.R., Yang, W.P, Meng, I.H.: -> Yeh et al
此外,请随时阅读String的文档,并浏览Regular Expressions的Oracle教程。我承认它有点长,但正则表达式太强大了,它们真的值得!