java 親測正則表達式獲取 大括號小括號內容,判斷數字和小數

獲取大括號小括號內容

項目開發用到了,暫做個簡單記錄

	private static String regex = "\\{([^}]*)\\}";//匹配大括號
	private static String regexx = "\\(([^}]*)\\)";//匹配小括號
	public static void main(String[] args) {
		String dakuohao = "{a+b}={c+d}>{d}";
		Pattern compile = Pattern.compile(regex);
		Matcher matcher = compile.matcher(dakuohao);
		while(matcher.find()){
			String group = matcher.group();
			System.out.print(group+";");
		}
		
		System.out.println();
		
		String xiaokuohao = "(a+b)=(c+d)>(d)";
		Pattern comp = Pattern.compile(regex);
		Matcher mat = comp.matcher(dakuohao);
		while(mat.find()){
			String group = mat.group();
			System.out.print(group+";");
		}
	}

匹配大括號和小括號的表達式,只有轉義後面的符號變了,是不是也可以換成別的
對稱的符號呢
在這裏插入圖片描述

判斷數字或者小數或數字小數混合

  • 整數 ^([0-9]{1,}[.][0-9]*)$
    在這裏插入圖片描述

  • 小數 ^([0-9]{1,}[.][0-9]*)$
    測試的時候我也找了不少博客,感覺多數人的都不能避免數字中的特殊符號

  • 在這裏插入圖片描述

  • 小數和數字混合 (^[0-9]*$)|(^([0-9]{1,}[.][0-9]*)$)

在這裏插入圖片描述

若還有別的正則,後續補充

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章