JAVA正则表达式失败
我有以下格式的字符串:JAVA正则表达式失败,java,regex,string,matcher,Java,Regex,String,Matcher,我有以下格式的字符串: ;1=2011-10-23T16:16:53+0530;2=2011-10-23T16:16:53+0530;3=2011-10-23T16:16:53+0530;4=2011-10-23T16:16:53+0530 Pattern pattern = Pattern.compile("(;1+)=(\\w+);"); String strFound= ""; Matcher matcher = pattern.matcher(strindData); while (m
;1=2011-10-23T16:16:53+0530;2=2011-10-23T16:16:53+0530;3=2011-10-23T16:16:53+0530;4=2011-10-23T16:16:53+0530代码>
Pattern pattern = Pattern.compile("(;1+)=(\\w+);");
String strFound= "";
Matcher matcher = pattern.matcher(strindData);
while (matcher.find()) {
strFound= matcher.group(2);
}
我编写了以下代码来查找中的字符串2011-10-23T16:16:53+0530
(;1=2011-10-23T16:16:53+0530;)
但它并没有像预期的那样发挥作用。你能给我一些提示吗
你能给我一些提示吗
对。-
,或:
,或+
都不是\w
的一部分,您必须使用正则表达式吗?为什么不调用String.split()。然后再次调用它,用等号将这些块分开。此时,您将得到一个整数和字符串形式的日期。从那里你可以看到绳子
我对输入做了一些更改,但只是出于演示的原因
您可以尝试以下方法:
String input = " ;1=2011-10-23T16:16:53+0530; 2=2011-10-23T16:17:53+0530;3=2011-10-23T16:18:53+0530;4=2011-10-23T16:19:53+0530;";
Pattern p = Pattern.compile("(;\\d+?)?=(.+?);");
Matcher m = p.matcher(input);
while(m.find()){
System.out.println(m.group(2));
}
String input = " ;1=2011-10-23T16:16:53+0530; 2=2011-10-23T16:17:53+0530;3=2011-10-23T16:18:53+0530;4=2011-10-23T16:19:53+0530;";
Pattern p = Pattern.compile("(;\\d+?)?=(.+?);");
Matcher m = p.matcher(input);
while(m.find()){
System.out.println(m.group(2));
}