獲取大括號小括號內容
項目開發用到了,暫做個簡單記錄
private static String regex = "http://{([^}]*)//}";//匹配大括號 private static String regexx = "http://(([^}]*)//)";//匹配小括號 public static void main(String[] args) { String dakuohao = "{a+b}={c+d}>70qarhlowxag"; 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]*)$)
ps:java使用正則表達式提取小括號中的內容
public class Test { public static List<String> getMsg(String msg) { List<String> list = new ArrayList<String>(); Pattern p = Pattern.compile("(//()([0-9a-zA-Z//./////=])*(//))"); Matcher m = p.matcher(msg); while (m.find()) { list.add(m.group(0).substring(1, m.group().length() - 1)); } return list; } public static void main(String[] args) throws Exception { String msg = "mSurface=Surface(name=com.bbk.launcher2/com.bbk.launcher2.Launcher)"; List<String> list = getMsg(msg); System.out.println(list); } }
總結
以上所述是小編給大家介紹的java正則表達式獲取大括號小括號內容并判斷數字和小數親測可用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
新聞熱點
疑難解答
圖片精選