我的日期格式如下。
示例:
由此:我想用
生成一个新查询我想搜索这些字符串,并希望用日期[%s,%s]替换此部分。 可以用Java中的Regex吗?
答案 0 :(得分:1)
您可以尝试使用以下正则表达式替换[%s,%s]
(dates)\\s*(?:\\[\\d{4}_\\d{2}_\\d{2},\\d{4}_\\d{2}_\\d{2}\\]|\\d{4}_\\d{2}_\\d{2}|last_\\d{2}_days)
替换字符串:
$1 [%s,%s]
String s = "select * from someTable t where t.name is not null and dates[2014_09_01,2014_09_15]\n" +
"select * from someTable t where t.name is not null and dates 2014_09_01\n" +
"select * from someTable t where t.name is not null and dates last_30_days\n";
System.out.println(s.replaceAll("(dates)\\s*(?:(?:\\[\\d{4}_\\d{2}_\\d{2},)?\\d{4}_\\d{2}_\\d{2}\\]?|last_\\d{2}_days)", "$1 [%s,%s]"));
<强>输出:强>
select * from someTable t where t.name is not null and dates [%s,%s]
select * from someTable t where t.name is not null and dates [%s,%s]
select * from someTable t where t.name is not null and dates [%s,%s]