정규 표현 식 - 자바 구현 - \ d, \ D, \ w, \ W, +, *,?

4124 단어 정규 표현 식
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
		*/


	}
}

좋은 웹페이지 즐겨찾기