这是如何从可变大小的字符串中获得所需字符的延续?但包括从中提取人物姓名的其他方案
pot-1_Sam [Sam is the word to be extracted]
pot_444_Jack [Jack is the word to be extracted]
pot_US-1_Sam [Sam is the word to be extracted]
pot_RUS_444_Jack[Jack is the word to be extracted]
pot_UK_3_Nick_Samuel[Nick_Samuel is the word to be extracted]
pot_8_James_Baldwin[James_Baldwin is the word to be extracted]
pot_8_Jack_Furleng_Derik[Jack_Furleng_Derik is the word to be extracted]
上面是示例单词,将从中提取人的名字。需要注意的一件事是,人名总是以“数字”和“下划线”开头。如何使用Java实现以上目标?
String[] strings = {
"pot-1_Sam",
"pot_444_Jack",
"pot_US-1_Sam",
"pot_RUS_444_Jack",
"pot_UK_3_Nick_Samuel",
"pot_8_James_Baldwin",
"pot_8_Jack_Furleng_Derik"
};
Pattern pattern = Pattern.compile("\\d_(\\w+)$");
for (String s : strings ){
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句