加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式

发布时间:2020-12-13 19:36:11 所属栏目:百科 来源:网络整理
导读:正则表达式基础 import java.util.regex.*;public class test2{public static void main(String[] args){/* //正则表达式 System.out.println("abc".matches("..."));p("ab1234c".replaceAll("d","-")); //将字符串中的数字用 - 代替Pattern pa=Pattern.co


正则表达式基础


 
import java.util.regex.*;


public class test2{
	public static void main(String[] args){
		
		/*
		

//正则表达式

System.out.println("abc".matches("...")); p("ab1234c".replaceAll("d","-")); //将字符串中的数字用 - 代替 Pattern pa=Pattern.compile("[a-z]{3}"); Matcher m=pa.matcher("sdf"); p(m.matches()); */ /* p("a".matches(".")); p("aa".matches("aa")); p("aaaa".matches("a*")); p("aaaa".matches("a+")); p("".matches("a*")); p("aaaa".matches("a?")); p("".matches("a?")); p("a".matches("a?")); p("123327548365456".matches("d{3,100}")); //判断数字是否出现3到100次 p("192.168.0.aaa".matches("d{1,3}.d{1,3}")); p("192".matches("[0-2][0-9][0-9]")); //判断是否三位数字,且第一位在0-2之间,第二、三位是0-9之间, */ /*

//范围

p("a".matches("[abc]")); p("a".matches("[^abc]")); //除了abc的 //下面三句等价 p("A".matches("[a-zA-Z]")); p("A".matches("[a-z]|[A-Z]")); p("A".matches("[a-z[A-Z]]")); p("R".matches("[A-Z&&[RFG]]")); */ /*

//认识s w d

p(" nrt".matches("s{4}")); //空格出现4次 p(" ".matches("S")); p("a_8".matches("w{3}")); p("abc888&^%".matches("[a-z]{1,3}d+[&^%#]+"));//a-z之间的字母出现1-3次,数字出现一次或多次,&^%#出现一次或多次 // p("".matches("")); p("".matches("\")); */ /*

//POSIX Style

p("a".matches("p{Lower}")); */ /*

//boundary

//System.out.println("abc".matches("...")); p("hello sir".matches("^h.*")); // "^"出现在 []中的开头才表示取反!这里表示一行的开头 p("hello sir".matches(".*ir$"));//表示以ir 结尾 p("hello sir".matches("^h[a-z]{1,3}ob.*")); //b表示分界 p("hellosir".matches("^h[a-z]{1,3}ob.*")); p(" n".matches("^[s&&[^n]]*n$")); p("aaa 8888c".matches(".*d{4}.*")); p("aaa 8888c".matches(".*bd{4}.")); p("aaa8888c".matches(".*d{4}.")); p("aaa8888c".matches(".*bd{4}.")); */ /*

//email

p("asddsdsad@sdsd.com".matches("[w[.-]]+@[w[.-]]+.[w]+")); // . 表示 . */ /*

//matches find lookingAt

Pattern p=Pattern.compile("d{3,5}"); String s="123-23344-234-22"; Matcher m=p.matcher(s); p(m.matches()); //当s匹配到 “123-”时发现不匹配,就剩下了“23344-234-22” m.reset(); //被匹配的 “123-”被吐出来了 p(m.find()); //从当前位置开始找下一个匹配的字符串 p(m.start()+"-"+m.end()); //对于已经找到的匹配字符串,输出其起始位置和终点位置 p(m.find()); p(m.start()+"-"+m.end()); p(m.find()); p(m.start()+"-"+m.end()); p(m.find()); p(m.lookingAt()); //从头开始找下一个匹配的字符串 p(m.lookingAt()); p(m.lookingAt()); p(m.lookingAt()); */ /*

//replacement

Pattern p=Pattern.compile("java",Pattern.CASE_INSENSITIVE); Matcher m=p.matcher("java Java JAVa JaVa IloveJAVA Ihatejava hehe"); StringBuffer buf=new StringBuffer(); int i=0; while(m.find()){ i++; if(i%2==0){ m.appendReplacement(buf,"java"); }else{ m.appendReplacement(buf,"JAVA"); } } m.appendTail(buf); p(buf); */ /*

//group分组

Pattern p=Pattern.compile("(d{3,5})([a-z]{2})"); String s="123aa-34345bb-234cc-00"; Matcher m=p.matcher(s); while(m.find()){ p(m.group(0)); //p(m.group(1)); p(m.group(2)); } */ //qulifiers //greedy quantifiers // Pattern p=Pattern.compile(".{3,10}[0-9]"); //这里前面表示字符(包括数字)出现3-10次,亦可表示为Pattern p=Pattern.compile("(.{3,10})[0-9]"); //Reluctant quantifiers // Pattern p=Pattern.compile(".{3,10}?[0-9]"); //亦可表示为Pattern p=Pattern.compile("(.{3,10}?)[0-9]"); //Possessive quantifiers Pattern p=Pattern.compile(".{3,10}+[0-9]"); String s="aaaa5bbbb6"; Matcher m=p.matcher(s); if(m.find()) p(m.start()+"-"+m.end()); else p("not match!"); } public static void p(Object o){ System.out.println(o); } }





找出某页面的email地址

import java.util.regex.*;
import java.io.*;

public class EmailSpider{
	public static void main(String[] args){
		try{
			
		BufferedReader br=new BufferedReader(new FileReader("F:JavaTest正则表达式11.htm"));
		System.out.println("haha");
		String line="";
		
		while((line=br.readLine())!=null){
			parse(line);
			}
		
	}catch(Exception e){
		System.out.println("文件出错!");
		System.exit(-1);
		}
	}
		
		
		public static void parse(String line){
			try{
			Pattern p=Pattern.compile("[w[.-]]+@[w[.-]]+.[w]+");
			Matcher m=p.matcher(line);
			while(m.find()){
				System.out.println(m.group());
				}
			}
			catch(Exception e){
		System.out.println("匹配出错!");
		System.exit(-1);
		}
	}
}


统计某个文件夹下面代码的空格行数、注释行数、和代码行数


import java.util.regex.*;
import java.io.*;

public class codeCount{
	
	static long  normalLines=0;
	static long commentLines=0;
	static long whiteLines=0;
	
	public static void main(String[] args){
		
		File f=new File("F:JavaTest正则表达式");
		File[] codeFiles=f.listFiles();
		for(File child:codeFiles){
		if(child.getName().matches(".*.java$")){
			parse(child);
			}
		}
		System.out.println("normalLines:"+normalLines);
		System.out.println("commentLines:"+commentLines);
		System.out.println("whiteLines:"+whiteLines);
		
		}
		
		
		private static void parse(File f){
			BufferedReader br=null;
			boolean comment=false;
			try{
			br=new BufferedReader(new FileReader(f));
			String line="";
			while((line=br.readLine())!=null){
				line=line.trim();
				if(line.matches("^[s&&[^n]]*$"))             //readLine()时已经把换行给去掉了,所以查找空行的正则表达式时没有后面的换行匹配了
				   whiteLines++;
				else if(line.startsWith("//")){
					commentLines++;
				}
				
			else if(line.startsWith("/*")&&!line.endsWith("*/")){
				commentLines++;
				comment=true;
				
			}else if(line.startsWith("/*")&&line.endsWith("*/")){
				 commentLines++;
				}else if(true==comment){
				commentLines++;
				if(line.endsWith("*/"))
				   comment=false;
				}else{
					normalLines++;
					}
			}
		}catch(Exception e){
			System.out.println("出错啦!");
			e.printStackTrace();
			}finally{
				if(br!=null){
					try{
					br.close();
					br=null;
				}catch(IOException e){e.printStackTrace();
					}
					}
				}
				
		}
		
}
//aa

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读