黑马程序员技术交流社区
标题:
关于正则表达式问题
[打印本页]
作者:
战狼
时间:
2014-8-1 13:08
标题:
关于正则表达式问题
<FONT face=宋体>public class MatchesDemo {
public static void main(String[] args) {
String[] str = {
"penglei@qq.com",
"penglei@crizyit.org",
"penglei@sohu.com",
"penglei@abc.xx",};
String maileRe = "\\w{3,20}@\\w+\\.(com|org|cn|net|gov)";//{3,20}表示字符最小不少于3个,最大不少于20个
Pattern pa = Pattern.compile(maileRe);
Matcher m = null;
try {
for (String mail : str) {
if (m==null) {
m = pa.matcher(mail);
}else
{
m.reset(mail);
}
String result = mail+(m.matches() ? "是":"不是")+"一个有效地址";
System.out.println(result);
}
} catch (Exception e) {
// TODO: handle exception
}
String[] str1 = {
"123456789",
"135559",
"555556987",
"18982774361",
};
String mm = "^([18[09]])\\d{8}$";
Pattern pop = Pattern.compile(mm);
Matcher ma = null;
for (String num : str1) {
if (ma==null) {
ma = pop.matcher(num);
}else
{
m.reset(num);
}
String result = num + (ma.matches() ? "是" : "不是") +"一个有效电话";
System.out.println(result);
}
}
}</FONT>
复制代码
为什么我匹配电话号码始终不对
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2