import org.apache.oro.text.regex.MalformedPatternException
import org.apache.oro.text.regex.MatchResult
import org.apache.oro.text.regex.Pattern
import org.apache.oro.text.regex.PatternCompiler
import org.apache.oro.text.regex.PatternMatcher
import org.apache.oro.text.regex.Perl5Compiler
import org.apache.oro.text.regex.Perl5Matcher
public class Zhengze {
public static void main(String[] args) throws MalformedPatternException {
PatternCompiler compiler = new Perl5Compiler()
String str = ",,test,,,,test"
String argexp = ".*,{3}.*"
Pattern pattern = compiler.compile(argexp)
PatternMatcher matcher = new Perl5Matcher()
if(matcher.contains(str,pattern)) {
MatchResult result = matcher.getMatch()
for(int n = 0n <result.length()n++) {
System.out.println(result.group(n))
}
}else{
System.out.println("nothing")
}
}
}
String a = "2016-06-24 10:33:20"
String b = a.replaceAll("[-\\s:]", "")
System.out.println(b)
跟你简单说明下
正则表达式中[]中的内容表示匹配其中任何一个
\s 表示匹配任何不可见的空字符,但是在java中需要在前面再加一个转义字符\才能生效
给你一个正则表达式的学习和在线测试地址:
//学习地址 www.sub-273.com/demo/RegEx/regex.html
//测试地址 tool.oschina.net/regex
在测试地址里,你可以输入2016-06-24 10:33:20
然后正则表达式填
^(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)$
替换文本填 $1$2$3$4$5$6
点击匹配后点替换就可以看到替换后的结果了。
至于replaceAll里的写法,你得考虑转义字符要加\
String text = "2016-06-24 10:33:20"
System.out.println(text.replaceAll("^