java正则表达式


正则表达式

 

什么是正则表达式?

符合一定规则的表达式。

 

String中方法:matches(String regex);//告知此字符串是否匹配给定的正则表达式。

Matcher中方法:find();//将规则作用到字符串上,并进行符合规则的子串查找。group();//用于获取匹配后结果。

 

特点:用一些特定的符号来表示一些代码操作,这样就简化了书写。

 

好处:简化了对字符串的复杂操作。

 

弊端:符号定义越多,正则越长,阅读性越差。

 

:用小括号标识,每定义一个小括号,就是一个组,而且有自动编号,从1开始。想要使用已有的组可以通过  \n(n就是组的编号)的形式来获取。

 

注意:数组要加\\。从左括号开始数,有几个左括号就是有几组。

 

常见操作:

匹配:

          用的是String类中的matches方法。规则匹配整个字符串,只要有一处不符合规则,匹配就结束,返回false。

 

       示例:对QQ号码进行校验

<span style="color:#000000;">	String qq = "265879541";
	String reg = "[1-9][0-9]{4,14}";
	boolean b = qq.matches(reg);//将正则和字符串关联对字符串进行匹配。
</span>


 

切割:

         用的是String类中的split方法。

 

     示例:按叠词进行切割

 

		String str = "hhellloooooo";
		String reg = "(.)\\1+";
		String[] arr = str.split(reg);
		System.out.println(arr.length);
		for (String s : arr) {
			System.out.print(" "+s);
		}


 

替换:

        用的是String类中的replaceAll方法。

 

    示例:将重叠的字符替换成单个字母。

		 String str = "heelloo wooord";
		 String reg = "(.)\\1+";
		 String s = str.replaceAll(reg, "$1");
		 System.out.println(s);


 

获取:

         将字符串中的符合规则的子串取出。

 

         1).将正则表达式封装成对象。使用的是Pattern中静态方法 compile(regex);

 

         2).让正则对象和要操作的字符串相关联,并获取正则匹配引擎。

                通过Pattern对象获取Matcher对象。

                通过Pattern对象中的matcher方法。该方法可以正则规则和字符串想关联。并返回匹配器对象(也就是匹配引擎)。

 

        3).通过引擎对符合规则的子串进行操作。使用Matcher对象中的方法。

示例:

		String str = "ming tian shi xing qi tian.";
		System.out.println(str);
		String reg = "\\b[a-z]{4}\\b";
		//将规则封装成对象。
		Pattern p = Pattern.compile(reg);
		//让正则对象和要作用的字符串相关联。获取匹配器对象。
		Matcher m  = p.matcher(str);
		while(m.find())
		{
			System.out.println(m.group());
			System.out.println("位置:"+m.start()+"~"+m.end());
		}


 

这四种操作都什么时候用呢?

         1.想知道该字符是否对是错,使用匹配。

         2.想要将已有的字符串变成另一个字符串,替换。

         3.想要按照自定的方式将字符串变成多个字符串。切割。获取规则以外的子串。

         4.想要拿到符合需求的字符串子串,获取。获取符合规则的子串。

 

代码示例

示例1:对邮件地址进行校验。

		String mail = "abc12@sina.com";
		mail = "1@1.1";
		
		String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";//较为精确的匹配。
		reg = "\\w+@\\w+(<a target=_blank target="_blank" href="file://\\.\\w+)+';//">\\.\\w+)+";//</a>相对不太精确的匹配。
		
		System.out.println(mail.matches(reg));


 

示例2:将下列字符串转成:我要学编程

		String str = "我我...我我...我要..要要...要要...学学学....学学...编编编...编程..程.程程...程...程";
		str = str.replaceAll("\\.+","");
		str = str.replaceAll("(.)\\1+","$1");
		System.out.println(str);


 

示例3192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30    ip地址进行地址段顺序的排序。

		String ip = "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30";
		ip = ip.replaceAll("(\\d+)","00$1");
		ip = ip.replaceAll("0*(\\d{3})","$1");

		String[] arr = ip.split(" ");
		TreeSet<String> ts = new TreeSet<String>();
		for(String s : arr)
		{
			ts.add(s);
		}
		for(String s : ts)
		{
			System.out.println(s.replaceAll("0*(\\d+)","$1"));
		}


 

网页爬虫(蜘蛛)

 

功能是获取互联网上的一些指定的信息。

 

示例:

<span style="font-size:14px;">import java.io.*;
import java.util.regex.*;
import java.net.*;
class RegexTest
{
	public static void main(String[] args) throws Exception
	{
		getMails_1();//
		getMails();
	}
	/*
	获取指定网页中的邮件地址。
	*/
	public static void getMails_1()throws Exception
	{
		URL url = new URL("http://192.168.1.254:8080/myweb/mail.html");
		URLConnection conn = url.openConnection();
		BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		String line = null;
		String mailreg = "\\w+@\\w+(\\.\\w+)+";
		Pattern p = Pattern.compile(mailreg);
		while((line=bufIn.readLine())!=null)
		{
			Matcher m = p.matcher(line);
			while(m.find())
			{
				System.out.println(m.group());
			}
		}
	}
	/*
	获取指定文档中的邮件地址。
	使用获取功能。Pattern  Matcher
	*/
	public static void getMails()throws Exception
	{
		BufferedReader bufr = new BufferedReader(new FileReader("mail.txt"));
		String line = null;
		String mailreg = "\\w+@\\w+(\\.\\w+)+";
		Pattern p = Pattern.compile(mailreg);
		while((line=bufr.readLine())!=null)
		{
			Matcher m = p.matcher(line);
			while(m.find())
			{
				System.out.println(m.group());
			}
		}
	}
}
</span>


 

 

总结:

 

    正则表达式其实是用来操作字符串的一些规则。学习正则表达式,就是在学习一些特殊符号的使用。其实String类中的matches方法用的就是Pattern和Matcher对象来完成的。只不过被String的方法封装后,用起来较为简单。但是功能却单一。

    Pattern用于描述正则表达式,可以对正则表达式进行解析。

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值