你能解释一下这个正则表达式在这段代码中是如何工作的吗? [英] Can you please explain how this regex work in this code ?

查看:96
本文介绍了你能解释一下这个正则表达式在这段代码中是如何工作的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Math {
	public static void compare(String A, String B) {
		int a;
	    a= S.replaceAll("[^" + J + "]" , "").length();
	String b = S.replaceAll("[^"+ J +"]", "");
	    System.out.println(a is +a);
	    System.out.println(b is +b);
	    
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String A= "AHhB";
		String B="CKDADEGEGAh";
	Math.compare(J, S);
	}<pre lang="java">





}



我尝试过:



输出:a是3

b是AAh

我的理解:[^ J]匹配不在字符串J中的字符。

但是[^和]



}

What I have tried:

Output :a is 3
b is AAh
My Understanding : [^J] matches a characters which is not in String J.
But what is the use of "[^" and "]"

推荐答案

它表示括号中不在集合中的所有字符。所以 [^ abc] 表示除 a 之外的任何字符 b c 。请参阅 RegExr:Learn,Build,&测试RegEx [ ^ ]
It means all characters that are not in the set in brackets. So [^abc] means any character except a or b or c. See RegExr: Learn, Build, & Test RegEx[^]


这篇关于你能解释一下这个正则表达式在这段代码中是如何工作的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆