|
|
获取大括号小括号内容
. | N: R) s! e项目开发用到了,暂做个简单记录- 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+";"); } }
复制代码 匹配大括号和小括号的表达式,只有转义后面的符号变了,是不是也可以换成别的4 B8 N3 n( j! c
对称的符号呢2 @, j9 s. r& H0 ?
8 H+ e* x b. ]4 K3 Z" x9 d; z
判断数字或者小数或数字小数混合
; }0 e8 H$ Q9 n& D$ G' x" r整数 ^([0-9]{1,}[.][0-9]*)$
1 i* C _, }$ ~# ]. Z x. G) ?( W& V( K/ ^+ s
小数 ^([0-9]{1,}[.][0-9]*)$4 D7 o# _0 o. w$ K8 W. Z: Z
测试的时候我也找了不少博客,感觉多数人的都不能避免数字中的特殊符号" H; ]$ z: C" s$ q
% j( U; M! r4 \小数和数字混合 (^[0-9]*$)|(^([0-9]{1,}[.][0-9]*)$)/ Z% w& D n) H3 l
: ?' u+ L! |! R6 Y+ N* Mps: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); } }
复制代码 总结+ y1 ~" m* e# \' l0 d* c g3 Y$ D
以上所述是小编给大家介绍的java正则表达式获取大括号小括号内容并判断数字和小数亲测可用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!3 c- B+ z4 E' B7 q4 V( ?
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!3 I/ o7 C G6 Q- t
2 I" ]. S" `# v来源:http://www.jb51.net/article/163975.htm
! {* a! t# J. C免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|