在我的助手类中,我从日志文件中提取密钥字符串。我在那里搜索日期和子字符串该文本。问题是当我使用 java 日期包含那个包含日期的短语时,硬编码的短语以不同的方式起作用。代码片段:BufferedReader br = new BufferedReader(new FileReader("/developer.log")); StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); line = br.readLine(); } String second = sb.toString(); String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = "]"+simpleDateFormat.format(new Date()); Matcher m2 = Pattern.compile("^(.*)date(.*)$").matcher(second); if (m2.find()) { String keyPrefix = "Bearer "; key = keyPrefix + m2.group(1); } } br.close(); return key;该date变量不返回模式匹配,但是当我在]2019-03-01工作时对字符串进行硬编码时。这里可能有什么问题?提前致谢。
1 回答
泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
您匹配固定模式“日期”。
要匹配创建的日期字符串,您必须将匹配器创建为:
Matcher m2 = Pattern.compile("^(.*)"+date+"(.*)$").matcher(second);添加回答
举报
0/150
提交
取消
