|
|
获取大括号小括号内容
( @) u; c/ [* f# }8 Q项目开发用到了,暂做个简单记录- 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+";"); } }
复制代码 匹配大括号和小括号的表达式,只有转义后面的符号变了,是不是也可以换成别的
+ _" _5 e5 `* h, c2 @4 M# _( U对称的符号呢 s; r- G9 ]8 g8 p# |# L8 @
) |9 q! _, V& L7 K* |判断数字或者小数或数字小数混合4 U1 z# X. E7 Q
整数 ^([0-9]{1,}[.][0-9]*)$; N. b" K9 {- ^& Z d; n
) n2 e a3 }- ^! f- \8 U$ M
小数 ^([0-9]{1,}[.][0-9]*)$
]5 H+ o- R A2 s5 T) Y测试的时候我也找了不少博客,感觉多数人的都不能避免数字中的特殊符号
# q6 o( X, w u* G# H# Z( N0 @8 ~3 p- m. K# Y8 T) C
小数和数字混合 (^[0-9]*$)|(^([0-9]{1,}[.][0-9]*)$)" D- @" i! S- V. ]+ m" X
. s) l) S+ Y& ^ O; b5 T* x
ps:java使用正则表达式提取小括号中的内容- public class Test { public static List getMsg(String msg) { List list = new ArrayList(); 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 list = getMsg(msg); System.out.println(list); } }
复制代码 总结
( q/ W6 h7 `5 B+ ~ I, c以上所述是小编给大家介绍的java正则表达式获取大括号小括号内容并判断数字和小数亲测可用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!# Y% Z( k. H0 g, S! r6 E
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
! J3 l8 F2 d9 V7 B/ s1 `& s4 u( }- S! F, ]3 s2 ]6 \" p3 B' V
来源:http://www.jb51.net/article/163975.htm
- h3 e% N& X: L* ]免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|