`
skyzh
  • 浏览: 11011 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 正则表达式

阅读更多

去除重复值

String regex = "(\\w+)((\\s)\\1)*";
String text = "one one one";
Pattern pattern = new Pattern(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
    System.out.println(matcher.group(0) );
    System.out.println(”starts at“ + matcher.start() );
    System.out.println(“ends at ” + matcher.end() );
}
Replacer replacer = pattern.replacer("$1");
System.out.println(replacer.replace(text));

输出:
one one one    starts at 0    ends at 11    length 11  
one

//金额三位分割

p = Pattern.compile("(?<=(\\d))(?=(?:\\d{3})+$)");
matcher=p.matcher("1234567890");
System.out.println(matcher.replaceAll(","));

 

// 去掉<>标签及其之间的内容
        p = Pattern.compile("(<[^>]*>)");
        m = p.matcher(str);
        String temp = str;
        //下面的while循环式进行循环匹配替换,把找到的所有
        //符合匹配规则的字串都替换为你想替换的内容
        while (m.find()) {
            value = m.group(0);
            temp = temp.replace(value, "要替换的内容");
        }

 

//正则 匹配例如 "${jljkljsd98_23u749823}" 即不能包含 "中文" ;" ,"; " \ "
  String regex="\\$\\{\\w*\\}$";

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics