正则表达式-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 [email protected]. For questions about a book use [email protected]. Feel " + 
			"free to send unsolicated email to [email protected] (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());
		}
		/*
		[email protected]
		[email protected]
		[email protected]
		*/
		System.out.println();

		//将上面的字符串中的邮箱做修改
		String str2 = "Send personal email to [email protected] or [email protected]." +
			" For questions about a book use [email protected]. " + 
			"If your message is urgent try [email protected]. " +
			"Feel free to send unsolicated email to [email protected] " +
			"(wouldn't it be nice if it were that simple, huh?).";
		m = p.matcher(str2);
		while (m.find()){
			System.out.println(m.group());
		}
		/*
		[email protected]
		[email protected]
		[email protected]
		[email protected]
		[email protected]
		*/
		System.out.println();
		//并没有匹配出完整的邮箱 [email protected][email protected]
		//因为 \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());
		}
		/*
		[email protected]
		[email protected]
		[email protected]
		[email protected]
		[email protected]
		*/
	}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class RepeatMatch2{
	public static void main(String[] args){
		String str = "Hello [email protected] 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());
		}
		//[email protected]
		//这是不符合规定的,因为邮箱第一个字母是不允许为 . 的,也就是说第一个必须是字母或者数字
		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());
		}
		//[email protected]
	}
}
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
		*/


	}
}

你可能感兴趣的:(正则表达式)