浏览 3788 次
锁定老帖子 主题:java正则 实例
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-11-05
java的正则很强大,谁用谁知道
用到最多的还是ismatch,看看是否符合正则
还可以用它替代字符串,过滤非法字符,截取字符串,分割字符串
替代字符串: 替代回车符号
Pattern ilSingle = Pattern.compile("\r|\n"); String message = "xxxxxx"; message = ilSingle.matcher(message).replaceAll("");
截取字符串: expected:xxxxxx but was:xxxxxxx
Pattern p = Pattern.compile("(expected:)(.*?)(but was:)(.*)"); Matcher m = p.matcher(message); PigErrorBean bean = new PigErrorBean(); if(m.find()){ bean.setExpect(m.group(2)); bean.setOutput(m.group(4)); } return bean;
分割字符串: (1,2,3),(6,5,4),(95,54,23),(44,53,54,12,43),(1,2),(2)
List<List<Object>> paramList = new LinkedList<List<Object>>(); Pattern pattern = Pattern.compile("(\\()(.*?)(\\))"); Matcher matcher = pattern.matcher(params); boolean hasMatcher = false; while (matcher.find()) { hasMatcher = true; List<Object> list = new LinkedList<Object>(); String strList = matcher.group(2); StringTokenizer st = new StringTokenizer(strList,","); while (st.hasMoreElements()) { Object param = st.nextElement(); list.add(param); } paramList.add(list); }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |