正则表达式-Java实现 - \d、\D、\w、\W、+、*、?

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
	\d 的含义是 [0-9]
	\D 的含义是 [^0-9]
	感觉最好的方法还是先写好正则表达式,在将它转成 java 语法支持的内容
*/

public class MatchNumber {
	public static void main(String[] args) {
		Pattern p = Pattern.compile("myArray\\[\\d\\]");
		Matcher m = p.matcher("var myArray = new Array(); if (myArray[0] == 0){} if (myArray[1] == 1){}");
		System.out.println(m.pattern()); //myArray\[\d\]
		while (m.find()){
			System.out.println(m.group());
		}
		/*
		myArray[0]
		myArray[1]
		*/

	}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
	\w 任何一个字母数字字符(大小写均可)或下划线,等价于 [A-Za-z0-9_]
	\W 表示[^A-Za-z0-9_]
*/

public class MatchAlphanum{
	public static void main(String[] args){
		String str = "112132 A1C2E3 48075 48237 M1B4F2 90046 H1H2H3";
		// \w\d\w\d
		Pattern p = Pattern.compile("\\w\\d\\w\\d\\w\\d");
		Matcher m = p.matcher(str);
		while (m.find()){
			System.out.println(m.group());
		}
		/*
		112132
		A1C2E3
		M1B4F2
		H1H2H3
		*/
		System.out.println();
	}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
	+ 表示匹配一个或多个字符(至少一个,不匹配零个字符的情况)
	邮箱中是允许出现下划线和 . 的
*/

public class RepeatMatch1{
	public static void main(String[] args){
		String str = "Send personal email to ben@forta.com. For questions about a book use support@forta.com. Feel " + 
			"free to send unsolicated email to spam@forta.com (wouldn't it be nnice if it were that simple, huh?).";
		//匹配邮箱 \w+@\w+\.\w+
		Pattern p = Pattern.compile("\\w+@\\w+\\.\\w+");
		Matcher m = p.matcher(str);
		while (m.find()){
			System.out.println(m.group());
		}
		/*
		ben@forta.com
		support@forta.com
		spam@forta.com
		*/
		System.out.println();

		//将上面的字符串中的邮箱做修改
		String str2 = "Send personal email to ben@forta.com or bean.forta@forta.com." +
			" For questions about a book use support@forta.com. " + 
			"If your message is urgent try ben@urgent.forta.com. " +
			"Feel free to send unsolicated email to spam@forta.com " +
			"(wouldn't it be nice if it were that simple, huh?).";
		m = p.matcher(str2);
		while (m.find()){
			System.out.println(m.group());
		}
		/*
		ben@forta.com
		forta@forta.com
		support@forta.com
		ben@urgent.forta
		spam@forta.com
		*/
		System.out.println();
		//并没有匹配出完整的邮箱 bean.forta@forta.com、ben@urgent.forta.com
		//因为 \w 能够匹配的内容是 字母、数字、下划线
		//改进,使用 [\w\.]+@[\w\.]+\.\w+

		p = Pattern.compile("[\\w\\.]+@[\\w\\.]+\\.\\w+");
		m = p.matcher(str2);
		System.out.println(m.pattern());
		while (m.find()){
			System.out.println(m.group());
		}
		/*
		ben@forta.com
		bean.forta@forta.com
		support@forta.com
		ben@urgent.forta.com
		spam@forta.com
		*/
	}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
	* 表示含有零个或者多个指定的字符
*/

public class RepeatMatch2{
	public static void main(String[] args){
		String str = "Hello .ben@forta.com is my email address.";
		//做一个邮箱的匹配 [\w\.]+@[\w\.]+\.\w+
		Pattern p = Pattern.compile("[\\w\\.]+@[\\w\\.]+\\.\\w+");
		Matcher m = p.matcher(str);
		while (m.find()){
			System.out.println(m.group());
		}
		//.ben@forta.com
		//这是不符合规定的,因为邮箱第一个字母是不允许为 . 的,也就是说第一个必须是字母或者数字
		System.out.println();


		// \w+[\w\.]*@[\w\.]+\.\w+
		p = Pattern.compile("\\w+[\\w\\.]*@[\\w\\.]+\\.\\w+");
		m = p.matcher(str);
		while (m.find()){
			System.out.println(m.group());
		}
		//ben@forta.com
	}
}
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/*
	? 表示匹配零个或者一个字符
*/

public class RepeatMatch3{
	public static void main(String[] args){
		String url = "The URL is http://www.forta.com/, to connect securely use https://www.forta.com/ instead.";
		//这里进行 URL 的匹配,两种 URL 第一个是 http 第二个是 https
		//http[s]?://[\w\.]+
		Pattern p = Pattern.compile("http[s]?://[\\w\\.]+");
		Matcher m = p.matcher(url);
		while (m.find()){
			System.out.println(m.group());
		}
		/*
		http://www.forta.com
		https://www.forta.com
		*/


	}
}
  • 18
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值