JDK regex 用法及用途

[list]
[*] 查找 Boolean flag = pattern.matcher("fda").find();
[*] 分割 String[] mm = pattern2.split(“say:”);
[*] 格式化 String format= pattern1.matcher(String.valueOf(vv)).group()
[*] 替换 String format= pattern3.matcher(ss1) .replaceAll("A")
[*] 匹配 String format= pattern5.matcher(email).matches()
[/list]


public class RegrexUtilTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String testStr="i am a good man, over!";
String patternStr = "over!$";
Pattern pattern = Pattern.compile(patternStr);
Boolean flag =pattern.matcher(testStr).find();
System.out.println("查找(find):"+testStr + "是以 over! 结尾的:"+flag);
System.out.println("查找(find):"+"我们一般使用 String.endWith :"+testStr.endsWith("over!"));

testStr="abcde|fff|gg";
patternStr = "\\|";
pattern = Pattern.compile(patternStr);
String targets[] =pattern.split(testStr);
System.out.print("分割(split): [");
boolean firstflag = true;
for(String target:targets){
if(firstflag){
System.out.print(target);
firstflag = false;
}
System.out.print(","+target);
}
System.out.println("]");
String target1s[] = testStr.split("\\|");

System.out.print("分割(split): 我们一般使用 String.split [");
firstflag = true;
for(String target:target1s){
if(firstflag){
System.out.print(target);
firstflag = false;
}
System.out.print(","+target);
}
System.out.println("]");

testStr = "abc3.14159526dd";
patternStr = "[0-9]+\\.[0-9]{2}";
pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(testStr);
while(matcher.find()){
System.out.println("格式化(group):"+matcher.group());
}
System.out.println("格式化(group): 我们一般使用 String.substring "+testStr.substring(3,7));

testStr = "abcdeabdsa";
patternStr = "a";
pattern = Pattern.compile(patternStr);
String result = pattern.matcher(testStr).replaceAll("A");
System.out.println("替换(replace):"+result);
System.out.println("替换(replace): 我们一般使用 String.replace "+result.replaceAll("a", "A"));

testStr = "fdafdjak@163.com";
patternStr = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
pattern = Pattern.compile(patternStr);
flag = pattern.matcher(testStr).matches();
System.out.println("匹配(matches):"+flag);
}
}




查找(find):i am a good man, over!是以 over! 结尾的:true
查找(find):我们一般使用 String.endWith :true
分割(split): [abcde,abcde,fff,gg]
分割(split): 我们一般使用 String.split [abcde,abcde,fff,gg]
格式化(group):3.14
格式化(group): 我们一般使用 String.substring 3.14
替换(replace):AbcdeAbdsA
替换(replace): 我们一般使用 String.replace AbcdeAbdsA
匹配(matches):true


可能你们会发现,这些功能可以用startWith,split,replace,substring等常用功能来代替,而且简单实用,除了匹配之外。

但是如果是不区分大小写,当true/yes/1代表一个意思时,正则就发挥作用了。

其实格式化 比如 yyyy-mm-dd, ##.##等等 都是格式化定义,最后都是需要格式化具体的数据。

匹配是正则用的最多的一个功能,手机号码,区号,QQ号码,e-mail,密码,用户名,等等需要用户输入的,如果有必要都要验证。

isPhone: "^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$"
isMobilePhone: "^((\\([0-9]{3}\\))|(\\d{3}-))?(13[456789][0-9]{8}|15[89][0-9]{8})+$"
isEmail: "^[\\w-]+(.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"
isLink: "^<a\\s[^>]*href=\"[^>]*>(.*?)</a>"
...

其他的欢迎补充。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值