正则表达式的常用写法

import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyRegular { public static void main(String[] args) { String regex = "★"; Pattern pattern = Pattern.compile(regex); String[] strings = { "abc", "abbc", "bc" }; strings = new String[] { "a", "e", "i", "o", "u" }; strings = new String[] { "a48", "b49" }; strings = new String[] { "abc", "abbc", "abbbc" }; Matcher matcher; regex = "a*(b+)c?"; regex = "[a|e|i|o|u]"; regex = "\\w+\\d+"; regex = "[年月日]"; regex = "元|人民币|RMB"; regex = "a*b+c?"; regex = "([a-zA-Z]{1,6}[0-9]{3})"; regex = "a(b{1,3})c"; regex = "\\s+"; regex = "<(\\w+)>(\\w+)</(\\w+)>"; regex = "[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"; pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); pattern = Pattern.compile(regex); String newChar = "¥"; String string = "2011年10月13日"; strings = pattern.split(string); string = "10元10人民币10RMB".replaceAll(regex, newChar); string = "abbbc abbc abc"; string = "tomcat127"; string = "name=scott pass=tiger"; string = "<name>scott</name><pass>tiger</pass>"; string = "scott@sina.com"; StringBuffer buffer = new StringBuffer(); for (String input : strings) { matcher = pattern.matcher(input); System.out.println(matcher.matches()); if (Pattern.matches(regex, input)) { System.out.println(input); } } for (matcher = pattern.matcher(string); matcher.find(); matcher .appendReplacement(buffer, string)) { string = matcher.group().toUpperCase(); System.out.println(matcher.group()); } System.out.println(matcher.appendTail(buffer)); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值