java【基础】正则表达式

1 字符串判断

 java的正则使用的是Pattern以及Matcher来配合使用的。

   如果只是用来判断输入的字符串是否符合格式,推荐使用Matcher的matches方法。

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "service@qq.com";
        String regEx = "[\w]+@[\w]+\.com";
        Pattern pattren = Pattern.compile(regEx);
        Matcher matcher =pattren.matcher(str);
        System.out.println(matcher.matches());
        
    }

  如果是需要获取匹配的分组内容。推荐使用find和group

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "service@qq.com";
        String regEx = "([\w]+)@([\w]+\.)com";
        Pattern pattren = Pattern.compile(regEx);
        Matcher matcher =pattren.matcher(str);
        System.out.println(matcher.find());//true
        System.out.println(matcher.group(1));//service
        System.out.println(matcher.group(2));//qq.
        
    }

2字符串分割

使用pattren.split

public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "service@qq.com@x";
		String regEx = "@";
		Pattern pattren = Pattern.compile(regEx);
		String[] strs =pattren.split(str);
		StringBuffer buf = new StringBuffer();
		buf.append("[");
		for(int i=0;i<strs.length;i++) {
			buf.append(strs[i]);
			if(i<(strs.length-1)) {
				buf.append(",");
			}
		}
		buf.append("]");
		System.out.println(buf.toString());//[service,qq.com,x]
	}

3字符串替换

Matcher replaceFirst 找到第一个位置进行替换
replaceAll 替换所有位置
public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "service@qq.com@x";
        String regEx = "@";
        Pattern pattren = Pattern.compile(regEx);
        Matcher matcher = pattren.matcher(str);
        System.out.println(matcher.replaceFirst(",@"));//service,@qq.com@x
        System.out.println(matcher.replaceAll("?"));//service?qq.com?x
        
    }

上面的替换似乎完成了我们想要的功能,但是如果我任性点,我只想替换掉最后的一个@那我怎么办,java提供了比较自由的替换appendReplacement。

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "service@qq.com@x@y";
        String regEx = "@";
        Pattern pattren = Pattern.compile(regEx);
        Matcher matcher = pattren.matcher(str);
        //System.out.println(matcher.replaceFirst(",@"));
        //System.out.println(matcher.replaceAll("?"));
        StringBuffer sb = new StringBuffer();
        int index = 0;
        while(matcher.find()) {
            index++;
            if(index==2) {
                matcher.appendReplacement(sb, "?");
            }
        }
        matcher.appendTail(sb);
        System.out.println(sb.toString());//service@qq.com?x@y
        
    }

上面的是指定位置,这里我们配合分组来实现这样一个功能 我要给每个数字后面都加一个$符号 比如 12abc3deg623 我要置换为 12$abc3$deg623$

手册上:

替换字符串可能包含到以前匹配期间所捕获的子序列的引用:$g 每次出现时,都将被 group(g) 的计算结果替换。$ 之后的第一个数始终被视为组引用的一部分。如果后续的数可以形成合法组引用,则将被合并到 g 中。只有数字 '0' 到 '9' 被视为组引用的可能组件。例如,如果第二个组匹配字符串 "foo",则传递替换字符串 "$2bar" 将导致 "foobar" 被添加到字符串缓冲区。可能将美元符号 ($) 作为替换字符串中的字面值(通过前面使用一个反斜线 ($))包括进来。

注意,在替换字符串中使用反斜线 () 和美元符号 ($) 可能导致与作为字面值替换字符串时所产生的结果不同。美元符号可视为到如上所述已捕获子序列的引用,反斜线可用于转义替换字符串中的字面值字符。

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "12abc3deg623";
        String regEx = "^([\d]+)abc([\d]+)deg([\d]+)$";
        Pattern pattren = Pattern.compile(regEx);
        Matcher matcher = pattren.matcher(str);
        StringBuffer sb = new StringBuffer();
        //System.out.println(matcher.find());
        /*System.out.println(matcher.find());
        System.out.println(matcher.groupCount());*/
        while(matcher.find()) {
            matcher.appendReplacement(sb, "$1\$abc$2\$deg$3\$");
        }
        matcher.appendTail(sb);
        System.out.println(sb.toString());//12$abc3$deg623$
        
    }
原文地址:https://www.cnblogs.com/gavinjunftd/p/9322583.html