/*
* 正则表达式:
* 1.匹配:String matches方法;用规则匹配整个字符串,只要有一处不符合
* 规则,就匹配结束,返回false
* 例子:
* 匹配QQ号码(5-15位,0不能开头)
* [1-9][0-9]{0,14}","[1-9]\\d{4,14}"
*
* 匹配手机号码(13**,15**,18**)
* 1[358]\\d{9}
*
* 验证邮箱
* "[a-zA-Z0-9_]+@[a-zA-Z0-9]+\\.[a-zA-Z\\.]+"
* "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+"
* mail.indexOf("@")!=1
* mail.contains("@")
* "\\w+@\\w+(\\.\\w+)+"
* 将邮箱的@之后的字符串去点,就是获取到用户名
* "(\\w+)@\\w+(\\.\\w+)+"
* while(m.find){System.out.println(m.group(1));}
*
* 空行
* "^[\\s&&[^\\n]*\\n$]"
*
*
*
*
*
* 2.切割:String split
* (1)String regex=" +";//按照多个空格来切割
* (2)"jfeaaieoccxaaa" 按照叠词切割 "(.)\\1" 按照多叠词切割"(.)\\1+"
* 为了可以让规则可以被结果重用,可以将规则封装成一个组。用()完成。组的出现都有编号。
* 从1开始。想要使用已有的组可以通过\n(n就是组的编号)的形式来获取。
*
* 3.替换replaceAll(regex,newStr)
* (1)将字符串中的数字替换成* ("\\d{5,}","*")
* (2)将重叠的字符替换成单个字符 ("(.)\\1","$1")
*
* 4.获取:将字符串中符合规则的子串取出
* 使用的是用正则对象Pattern 和匹配器Matcher
范例:
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
步骤:
1,先将正则表达式编译成正则对象。使用的是Pattern类一个静态的方法。compile(regex);
2,让正则对象和要操作的字符串相关联,通过matcher方法完成,并返回匹配器对象。
3,通过匹配器对象的方法将正则模式作用到字符串上对字符串进行针对性的功能操作
*
*
*/
//获取网页中的邮箱
//网页爬虫(蜘蛛)
public static void getMail_1() throws Exception
{
BufferedReader bfr=new BufferedReader(new FileReader("html.txt"));
String str=null;
String mailregex="(\\w+)@\\w+(\\.\\w+)+";
Pattern p=Pattern.compile(mailregex);
while((str=bfr.readLine())!=null)
{
Matcher m=p.matcher(str);
while(m.find())
{
System.out.println(m.group());
}
}
}
public static void getMail_2() throws Exception
{int a=0;
for(int i=1;i<55;i++)
{
URL url=new URL("http://tieba.baidu.com/p/1293845602?pn="+i);
URLConnection conn=url.openConnection();
BufferedReader bfr=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str=null;
String mailregex="\\w+@\\w+(\\.\\w+)+";
Pattern p=Pattern.compile(mailregex);
while((str=bfr.readLine())!=null)
{
Matcher m=p.matcher(str);
while(m.find())
{a++;
System.out.print(m.group()+",");
}
}
}
System.out.println(a);
}
//查询代码工作量的小程序(可以测试自己每天的代码量哦,嘿嘿)
以//开头,被/**/包围的代码都是注释代码
空行
代码行
public class CodeCounter {
private static long normalLine = 0;// 代码行
private static long commentLines = 0;// 注释行
private static long whiteLines = 0;// 空白行
public static void main(String[] args) {
File f = new File("F:\\eclipse\\shili11111\\TankGame\\src\\myTankGame");
File file[] = f.listFiles();
for (File f2 : file) {
if (f2.getName().endsWith(".java"))// f2
{
count(f2);
}
}
System.out.println("normalLine:" + normalLine);
System.out.println("commentLines:" + commentLines);
System.out.println("whiteLines:" + whiteLines);
}
public static void count(File file) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
boolean b = false;
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.startsWith("//")
|| (line.startsWith("/*") && line.endsWith("*/"))) {
commentLines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
commentLines++;
b = true; // 这里使用的是一个标识符。只要是true那么这行代码就是在/**/中的注视代码
} else if (line.endsWith("*/")) {
commentLines++;
b = false;
} else {
if (b)
commentLines++;
else {
if (line.matches("^[\\s&&[^\\n]]*$"))// 一般空行的正则表达式是^[\\s&&[^\\n]]*\\n$
// 但是在readline中已经将\n去掉,所以这里的是没有\n的
{
whiteLines++;
} else {
normalLine++;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------------------- android培训、 java培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net/heima