正则表达式的一些应用

package test;
 
 
import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.sql.SQLClientInfoException;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.InflaterInputStream;
 
import javax.annotation.processing.FilerException;
import javax.management.RuntimeErrorException;
import javax.swing.text.AbstractDocument.LeafElement;

import privateclass.Filterby_Name;
import privateclass.Filterby_hidden;
import privateclass.Filterby_java;
import privateclass.MyBufferedReader;
import privateclass.Person;
 
public class Main {
 
	/*
	 * 正则表达式练习
	 */
    public static void main(String[] args) throws Exception {

    	practise1();
    	practise2();
    	practise3();
    	practise4();
    }

    /*
     * 治疗口吃
     * 我我我我  ...我我.我玉玉.. 玉玉玉树树..树树.临..临临临临风.风风风风
     * 
     */
    private static void practise1() {
    	String s = "我我我我  ...我我.我玉玉.. 玉玉玉树树..树树.临..临临临临风.风风风风";
    	s = s.replaceAll("\.", "");
    	s = s.replaceAll(" ", "");
    	s = s.replaceAll("(.)\1+", "$1");
    	System.out.println(s);
    	
    }

    /*
     * 将ip进行排序
     * 192.168.100.1 2.2.2.23 100.2.53.3 55.25.26.6
     */
	private static void practise2() {
		String s = "192.168.100.1 2.2.2.23 100.2.53.3 55.25.26.6";
		s = s.replaceAll("(\d+)", "00$1");
		s = s.replaceAll("0*(\d{3})", "$1");
		TreeSet<String> ts = new TreeSet<String>();
		String []str = s.split(" +");
		for(String name :str)
		{
			ts.add(name);
		}
		for(String name: ts)
		{
			System.out.println(name.replaceAll("0*(\d)", "$1"));
		}
		
	}

	/*
	 * 判断是否是邮箱
	 */
	private static void practise3() {
		
		String s = "9861805@qq.com.cn";
		String reg = "\w+@\w+(\.\w+)+";
		boolean b = s.matches(reg);
		System.out.println(b);
	}
	
	/*
	 * 网页爬虫找出邮箱
	 */
	private static void practise4() throws Exception {
		URL url = new URL("https://blog.csdn.net/weixin_39917347/article/details/81707106");
		BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
		
		Pattern p = Pattern.compile("\w+@\w+(\.\w+)+");
		
		String line = null;
		while((line = br.readLine()) != null)
		{
			Matcher m = p.matcher(line);
			
			while(m.find())
			{
				System.out.println(m.group());
			}
			
		}
		br.close();
		
	}


}

  

原文地址:https://www.cnblogs.com/WINDZLY/p/12398898.html