(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)

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

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

Greedy 数量
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n
X{n,} X,至少 n
X{n,m} 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 原文链接:https://www.f2er.com/regex/362338.html

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