(1)正则基础

前端之家收集整理的这篇文章主要介绍了(1)正则基础前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

包:java.util.regex

相关类:Pattern,Matcher,PatternSyntaxException

1. JDK API例子:

典型的调用顺序是 (*表示出现0次或多次;例子表示aaaaab是否匹配a*b格式)

 Pattern p = Pattern.("a*b");
 Matcher m = p.("aaaaab");
 boolean b = m.();compilematchermatches

在仅使用一次正则表达式时,可以方便地通过此类定义 matches 方法。此方法编译表达式并在单个调用中将输入序列与其匹配。语句

 boolean b = Pattern.matches("a*b","aaaaab");

String类的方法

      public boolean matches(Stringregex)
      boolean b = "aaaaab".matches("a*b");

2.常用简单方法(一些方法在String类中有实现)

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

public class RegexExamples {

	public static void main(String[] args) {
		// using pattern with flags
		Pattern pattern = Pattern.compile("ab",Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher("ABcabdAb");
		// using Matcher find(),group(),start() and end() methods
		while (matcher.find()) {
			System.out.println("Found the text \"" + matcher.group()
					+ "\" starting at " + matcher.start()
					+ " index and ending at index " + matcher.end());
		}

		// using Pattern split() method
		pattern = Pattern.compile("\\W");
		String[] words = pattern.split("one@two#three:four$five");
		System.out.print("Split using Pattern.split(): ");
		for (String s : words) {
			System.out.print(s + " ");
		}
		System.out.println();
		words = "one@two#three:four$five".split("\\W");
		System.out.print("Split using String.split(): ");
		for (String s : words) {
			System.out.print(s + " ");
		}
		System.out.println();

		// using Matcher.replaceFirst() and replaceAll() methods
		pattern = Pattern.compile("1*2");
		matcher = pattern.matcher("112345126782");
		System.out.println("Using matcher.replaceAll: "
				+ matcher.replaceAll("_"));
		System.out.println("Using matcher.replaceFirst: "
				+ matcher.replaceFirst("_"));

		System.out.println("Using String.replaceAll: "
				+ "112345126782".replaceAll("1*2","_"));
		System.out.println("Using String.replaceFirst: "
				+ "112345126782".replaceFirst("1*2","_"));
	}

}

输出

Found the text "AB" starting at 0 index and ending at index 2
Found the text "ab" starting at 3 index and ending at index 5
Found the text "Ab" starting at 6 index and ending at index 8
Split using Pattern.split(): one two three four five 
Split using String.split(): one two three four five 
Using matcher.replaceAll: _345_678_
Using matcher.replaceFirst: _345126782
Using String.replaceAll: _345_678_
Using String.replaceFirst: _345126782


3. 常用语法(API)

字符类
@H_403_49@[abc] @H_403_49@a、@H_403_49@b 或 @H_403_49@c(简单类)
@H_403_49@[^abc] 任何字符,除了 @H_403_49@a、@H_403_49@b 或 @H_403_49@c(否定)
@H_403_49@[a-zA-Z] @H_403_49@a 到 @H_403_49@z 或 @H_403_49@A 到 @H_403_49@Z,两头的字母包括在内(范围)
@H_403_49@[a-d[m-p]] @H_403_49@a 到 @H_403_49@d 或 @H_403_49@m 到 @H_403_49@p:@H_403_49@[a-dm-p](并集)
@H_403_49@[a-z&&[def]] @H_403_49@d、@H_403_49@e 或 @H_403_49@f(交集)
@H_403_49@[a-z&&[^bc]] @H_403_49@a 到 @H_403_49@z,除了 @H_403_49@b 和 @H_403_49@c:@H_403_49@[ad-z](减去)
@H_403_49@[a-z&&[^m-p]] @H_403_49@a 到 @H_403_49@z,而非 @H_403_49@m 到 @H_403_49@p:@H_403_49@[a-lq-z](减去)
预定义字符类
@H_403_49@. 任何字符(与行结束符可能匹配也可能不匹配)
@H_403_49@\d 数字:@H_403_49@[0-9]
@H_403_49@\D 非数字: @H_403_49@[^0-9]
@H_403_49@\s 空白字符:@H_403_49@[ \t\n\x0B\f\r]
@H_403_49@\S 非空白字符:@H_403_49@[^\s]
@H_403_49@\w 单词字符:@H_403_49@[a-zA-Z_0-9]
@H_403_49@\W 非单词字符:@H_403_49@[^\w]

边界匹配器
@H_403_49@^ 行的开头
@H_403_49@$ 行的结尾
@H_403_49@\b 单词边界
@H_403_49@\B 非单词边界
@H_403_49@\A 输入的开头
@H_403_49@\G 上一个匹配的结尾
@H_403_49@\Z 输入的结尾,仅用于最后的结束符(如果有的话)
@H_403_49@\z 输入的结尾

Greedy 数量
X@H_403_49@? X,一次或一次也没有
X@H_403_49@* X,零次或多次
X@H_403_49@+ X,一次或多次
X@H_403_49@{n@H_403_49@} X,恰好 n
X@H_403_49@{n@H_403_49@,} X,至少 n
X@H_403_49@{n@H_403_49@,m@H_403_49@} X,至少 n 次,但是不超过 m

几个例子

public class RegexExpression {
	
	public static void main(String[] args) {
		System.out.println("\"a\".matches(\"[abc]\") --> 匹配a/b/c: "
				+ "a".matches("[abc]"));
		System.out.println("\"aab\".matches(\"[abc]+\") --> a/b/c出现一次或多次: "
				+ "aab".matches("[abc]+"));
		System.out.println("\" @aab\".matches(\"\\s\\W?\\w{1,5}\") --> 空白字符+非单词字符至多一次+单词字符1到5次: "
				+ " @aab".matches("\\s\\W?\\w{1,5}"));
		System.out.println("\"xa@c2\".matches(\"^[^abc].+[123]$\") --> 非a/b/c开头+任意字符1次或多次+1/2/3结尾: "
				+ "xa@c2".matches("^[^abc].+[123]$"));
		
	}
}

输出

"a".matches("[abc]") --> 匹配a/b/c: true
"aab".matches("[abc]+") --> a/b/c出现一次或多次: true
" @aab".matches("\s\W?\w{1,5}") --> 空白字符+非单词字符至多一次+单词字符1到5次: true
"xa@c2".matches("^[^abc].+[123]$") --> 非a/b/c开头+任意字符1次或多次+1/2/3结尾: true
参考: http://www.jb51.cc/article/p-hnnbfqms-nd.html

猜你在找的正则表达式相关文章