正则表达式笔记

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

public class RegexTestHarness {

	public static void main(String[] args) {
		Console console = System.console();
		if (console == null) {
			System.err.println("No console.");
			System.exit(1);
		}

		while (true) {
			Pattern pattern = Pattern.compile(console
					.readLine("%nEnter your regex: "));
			Matcher matcher = pattern.matcher(console
					.readLine("Enter input string to search: "));
			boolean found = false;
			while (matcher.find()) {
				console.format("I found the text \"%s\" starting at index %d "
						+ "and ending at index %d.%n", matcher.group(),
						matcher.start(), matcher.end());
				found = true;
			}
			if (!found) {
				console.format("No match found.%n");
			}
		}
	}
}

cmd javac RegexTestHarness.java

cmd java RegexTestHarness

在cmd下测试正则。 



 
正则表达式笔记_第1张图片

 

元字符

API 所支持的元字符有:([{\^-$|}])?*+.

 

字符类
正则表达式笔记_第2张图片
 

预定义字符类
正则表达式笔记_第3张图片
 

你应该可以根据这张表指出前面每个匹配的逻辑:

  \d 匹配数字字符
  \s 匹配空白字符
  \w 匹配单词字符
  也可以使用意思正好相反的大写字母:
  \D 匹配非数字字符
  \S 匹配非空白字符
  \W 匹配非单词字符

 

量词
正则表达式笔记_第4张图片
 

边界匹配器
正则表达式笔记_第5张图片
 

反向引用

(/)

 

 


 
 

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