学习正则表达式参考书是《正则表达式必知必会》- Ben Forta 杨涛翻译
我是使用Java来联系里面的内容,所以在开始之前需要先了解两个Java用来编译正则表达式的类。
java .util.regex.Pattern;
java.util.regex.Pattern;
这里不做详细的介绍,在 API 文档中都有。将里面常用的方法,在下面的例子中呈现:
import java.util.regex.Pattern;
/*
*Pattern 类用于创建一个正则表达式,也可以说创建一个匹配模式,它的构造方法是私有的,不可以直接创建,但是可以通过
*Pattern.compile(regex String)简单工厂方法创建一个正则表达式
*/
public class TestPattern1{
public static void main(String[] args){
Pattern p = Pattern.compile("\\w+");
//pattern() Return the regular expression from which this pattern was compiled
System.out.println(p.pattern());
//split() The array of strings computed by splitting the input around matches of this pattern
String[] strs = p.split("我的QQ是:123456我的电话是:123456我的又像是aaa@aaaa.com");
for (int i = 0, len = strs.length; i < len; i++){
System.out.println("str[" + i + "]=" + strs[i]);
}
}
}
import java.util.regex.Pattern;
public class TestPattern2{
public static void main(String[] args){
System.out.println(Pattern.matches("\\d+", "2223")); //true
System.out.println(Pattern.matches("\\d+", "2223aa")); //false
System.out.println(Pattern.matches("\\d+", "22bb23")); //false
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestMatcher1{
public static void main(String[] args){
Pattern p = Pattern.compile("\\d+");
//matcher(input : CharSequence) Creates a matcher that will match the given input against this pattern.
Matcher m = p.matcher("22bb23");
//pattern() Returns the pattern that is interpreted by this matcher.
System.out.println(m.pattern()); // \d+
//matches() Attempts to match the entire region against the pattern.
System.out.println(m.matches()); //false
Matcher m2 = p.matcher("2233");
System.out.println(m2.matches()); //true
//lookingAt() Attempts to match the input sequence, starting at the beginning of the region, against the pattern.
System.out.println(m.lookingAt()); //true
Matcher m3 = p.matcher("aa2233");
System.out.println(m3.lookingAt()); //false
//find() Attempts to find the next subsequence of the input sequence that matches the pattern.
System.out.println(m.find()); //true
System.out.println(m2.find()); //false
System.out.println(m3.find()); //true
Matcher m4 = p.matcher("aabb");
System.out.println(m4.find()); //false
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestMatcher2{
public static void main(String[] args){
//当使用matches(),lookingAt(),find()执行匹配操作之后,就可以使用start()、lookingAt()、find()
//start() Returns the start index of the previous match.
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("aaa2223bb");
System.out.println(m.find()); //true
System.out.println(m.start()); //3
//end() Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.
System.out.println(m.end()); //7
//group() Returns the input subsequence captured by the given group during the previous match operation
System.out.println(m.group()); //2223
System.out.println();
Matcher m2 = p.matcher("2233aa");
System.out.println(m2.lookingAt());//false
System.out.println(m2.start()); //0
System.out.println(m2.end()); //4
System.out.println(m2.group()); //2233
System.out.println();
Matcher m3 = p.matcher("aa2233");
System.out.println(m3.lookingAt()); //false
//System.out.println(m3.start()); //出现异常,因为 lookingAt()没有找到内容,那么自然不可能再继续进行查找
System.out.println();
Matcher m4 = p.matcher("222356");
System.out.println(m4.matches()); //false
System.out.println(m4.start()); //0
System.out.println(m4.end()); //6
System.out.println(m4.group()); //222356
}
}
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Regex1{
public static void main(String[] args){
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("我的QQ是123456 我的电话是123456 我的邮箱是aaa@bbb.com");
while (m.find()){
System.out.println(m.group());
}
}
}
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Regex2{
public static void main(String[] args){
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("我的QQ是123456 我的电话是123456 我的邮箱是:aaaa@bbbb.com");
while (m.find()){
System.out.println(m.group());
System.out.print("start: " + m.start());
System.out.println(" end: " + m.end());
}
}
}