Java正则表达式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java正则表达式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1600字,纯文字阅读大概需要3分钟。
内容图文
![Java正则表达式](/upload/InfoBanner/zyjiaocheng/783/1f8ca83a97e4464499c692a5075b32b0.jpg)
我对正则表达式并不熟悉.
我希望获得以下常规例外的帮助:
1.字符串以alpha字开头,然后是任何字母或数字.例如Abc 1月20日至12月15日
2.十进制数的字符串.例如450,122,224.00
3.还要检查String是否包含任何类似’Page 2 of 20’的模式
谢谢.
解决方法:
// 1. String start with alpha word and then followed by
// any aplha or number. e.g. Abc 20 Jan to 15 Dec
// One or more alpha-characters, followed by a space,
// followed by some alpha-numeric character, followed by what ever
Pattern p = Pattern.compile("\\p{Alpha}+ \\p{Alnum}.*");
for (String s : new String[] {"Abc 20 Jan to 15 Dec", "hello world", "123 abc"})
System.out.println(s + " matches: " + p.matcher(s).matches());
// 2. String for a decimal number. e.g. 450,122,224.00
p = Pattern.compile(
"\\p{Digit}+(\\.\\p{Digit})?|" + // w/o thousand seps.
"\\p{Digit}{1,3}(,\\p{Digit}{3})*\\.\\p{Digit}+"); // w/ thousand seps.
for (String s : new String[] { "450", "122", "224.00", "450,122,224.00", "0.0.3" })
System.out.println(s + " matches: " + p.matcher(s).matches());
// 3. Also to check if String contain any pattern like 'Page 2 of 20'
// "Page" followed by one or more digits, followed by "of"
// followed by one or more digits.
p = Pattern.compile("Page \\p{Digit}+ of \\p{Digit}+");
for (String s : new String[] {"Page 2 of 20", "Page 2 of X"})
System.out.println(s + " matches: " + p.matcher(s).matches());
输出:
Abc 20 Jan to 15 Dec matches: true
hello world matches: true
123 abc matches: false
450 matches: true
122 matches: true
224.00 matches: true
450,122,224.00 matches: true
0.0.3 matches: false
Page 2 of 20 matches: true
Page 2 of X matches: false
内容总结
以上是互联网集市为您收集整理的Java正则表达式全部内容,希望文章能够帮你解决Java正则表达式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。