Java使用正則表達式搜索文本內所有的email地址并打印出來
Java使用正則表達式搜索文本內所有的email地址并打印出來
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern;public class SearchAddress {//TODO 創建查找地址類 /**
* @author liuxuewei */ public static void main(String []args) throws IOException{ try { BufferedReader in=new BufferedReader(new FileReader("<a href="F://Java">F://Java編程//text1.txt")); Pattern regex=Pattern.compile("(\\\\w[-.\\\\w]*\\\\@[-a-z0-9]+(\\\\.[-a-z0-9]+)*\\\\.(com|edu|info))", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE); String tempstr=new String(); String str=new String(); String find=null; while((tempstr=in.readLine())!=null){ str=str+tempstr+"\\n"; } Matcher mat=regex.matcher(str); while(mat.find()){//TODO 測試regex find=mat.group(1); System.out.println("Find:"+find); System.out.println("匹配的正則式:"+regex.pattern());//輸出匹配的正則表達式 } if(find==null) System.out.print("Can't Find"); } catch (FileNotFoundException e) { e.printStackTrace(); }
} }</pre>
本文由用戶 d6nd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!