Java之用正则表达式提取数据
要将字符串如11月7日10点前反馈提取数据进行存储,可使用正则表达式。
代码示例如下:
String actualFeedbackTime = "11月7日10点前反馈"; Pattern ptn = Pattern.compile("(\\d+)月(\\d+)日(\\d+)点前反馈"); Matcher matcher = ptn.matcher(actualFeedbackTime); LocalDateTime date = LocalDateTime.now(); if (matcher.matches()) { String month = matcher.group(1); String day = matcher.group(2); String hour = matcher.group(3); Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); date = LocalDateTime.of(year, Integer.parseInt(month), Integer.parseInt(day), Integer.parseInt(hour), 0, 0); }