我正在尝试提取以下字符串中的数字:
09/29/2014
我目前正在使用代码:
Pattern p = Pattern.compile("([0-9]{2})/([0-9]{2})/([0-9]{4})");
Matcher m = p.matcher(startDatepicker);
String startYear = m.group(3);
String startMonth = m.group(1);
String startDay = m.group(2);
startDatepicker
包含: 09/29/2014
但是,我没有收到任何比赛。.我也尝试用正斜杠转义,\\
但那也没有用。
我想念什么吗?
谢谢你的帮助。
在访问匹配的组之前,您需要调用find()
匹配器,并检查它是否找到匹配项:
Pattern p = Pattern.compile("([0-9]{2})/([0-9]{2})/([0-9]{4})");
Matcher m = p.matcher(startDatepicker);
if (!m.find()) {
return;
}
String startYear = m.group(3);
String startMonth = m.group(1);
String startDay = m.group(2);
m.find()
匹配器在第一个匹配项上的位置调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句