赞
踩
1 需求
2 接口
Class Pattern
public final class Pattern extends Object implements Serializable
java.lang.Object
java.util.regex.Pattern
- Field Detail
- public static final int UNIX_LINES
- public static final int CASE_INSENSITIVE
- public static final int COMMENTS
- public static final int MULTILINE
- public static final int LITERAL
- public static final int DOTALL
- public static final int UNICODE_CASE
- public static final int CANON_EQ
- public static final int UNICODE_CHARACTER_CLASS
- Method Detail
- public static Pattern compile(String regex)
- public static Pattern compile(String regex, int flags)
- flags - Match flags, a bit mask that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and COMMENTS
- DOTALL:In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.
- MULTILINE:In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence. 默认情况下,正则表达式 ^ 和 $ 忽略行结束符,仅分别与整个输入序列的开头和结尾匹配。如果激活 MULTILINE 模式,则 ^ 在输入的开头和行结束符之后(输入的结尾)才发生匹配。处于 MULTILINE 模式中时,$ 仅在行结束符之前或输入序列的结尾处匹配。
- public String pattern()
- public String toString()
- public Matcher matcher(CharSequence input)
- public int flags()
- public static boolean matches(String regex, CharSequence input)
- public String[] split(CharSequence input, int limit)
- public String[] split(CharSequence input)
- public static String quote(String s)
- public Predicate<String> asPredicate()
- public Stream<String> splitAsStream(CharSequence input)
Class Matcher
public final class Matcher extends Object implements MatchResult
java.lang.Object
java.util.regex.Matcher
- Method Detail
- public Pattern pattern()
- public MatchResult toMatchResult()
- public Matcher usePattern(Pattern newPattern)
- public Matcher reset()
- public Matcher reset(CharSequence input)
- public int start()
- public int start(int group)
- public int start(String name)
- public int end()
- public int end(int group)
- public int end(String name)
- public String group()
- public String group(int group)
- public String group(String name)
- public int groupCount()
- public boolean matches()
- public boolean find()
- public boolean find(int start)
- public boolean lookingAt()
- public static String quoteReplacement(String s)
- public Matcher appendReplacement(StringBuffer sb, String replacement)
- public StringBuffer appendTail(StringBuffer sb)
- public String replaceAll(String replacement)
- public String replaceFirst(String replacement)
- public Matcher region(int start, int end)
- public int regionStart()
- public int regionEnd()
- public boolean hasTransparentBounds()
- public Matcher useTransparentBounds(boolean b)
- public boolean hasAnchoringBounds()
- public Matcher useAnchoringBounds(boolean b)
- public String toString()
- public boolean hitEnd()
- public boolean requireEnd()
3.X 示例:字符串匹配 三种写法
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * 如下三种方式效果一样:
- * str.matches(regex)
- * Pattern.matches(regex, str)
- * Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches();
- */
- public class Test {
- public static void main(String[] args) {
- String string = "1949";
-
- // 方式一:String类 matches方法
- System.out.println(string.matches("19\\d\\d"));
-
- // 方式二:Pattern类 matches方法
- System.out.println(Pattern.matches("19\\d\\d", string));
-
- // 方式三:Pattern类 compile方法 matcher方法、Matcher类 matches方法
- Pattern pattern = Pattern.compile("19\\d\\d");
- Matcher matcher = pattern.matcher(string);
- System.out.println(matcher.matches());
- }
- }

3.X 示例代码: matches方法和find方法 区别
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * matches、find区别
- * matches:正则表达式必须与字符串完全匹配,才返回true
- * find:跟contains类似,只要找到符合正则表达式的子字符串,就返回true
- * <p>
- * 注意事项:
- * find匹配一次后,下次自动匹配下一个符合条件的子字符串
- */
- public class Test {
- public static void main(String[] args) {
- String string = "This is 1949";
-
- Pattern pattern = Pattern.compile("19\\d\\d");
- Matcher matcher = pattern.matcher(string);
- System.out.println(matcher.matches()); // false
- System.out.println(matcher.find()); // true
- System.out.println(matcher.find()); // false
- }
- }

3.X 示例:提取非分组匹配的数据(每组只有一个数据)
- import java.util.ArrayList;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * 分组匹配结果提取
- * matcher.group():整个正则表达式匹配的结果
- * matcher.group(0):返回整个正则表达式匹配的结果
- * matcher.group(1):返回第一个表达式匹配的结果
- * matcher.group(2): 返回第二个表达式匹配的结果
- */
- public class Test {
- public static void main(String[] args) {
- String string = "This is 1949, i am 18 years old. This is 1950, i am 19 years old.";
-
- ArrayList<String> arrayList = new ArrayList<String>();
-
- Pattern pattern = Pattern.compile("\\d*");
- Matcher matcher = pattern.matcher(string);
-
- while (matcher.find()) {
- arrayList.add(matcher.group());
- }
-
- for (int i = 0; i < arrayList.size(); i++) {
- System.out.println(i + ": " + arrayList.get(i));
- }
- }
- }

3.X 示例:提取分组匹配的数据(每组只有一个数据)
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * 分组匹配结果提取
- * matcher.group():整个正则表达式匹配的结果
- * matcher.group(0):返回整个正则表达式匹配的结果
- * matcher.group(1):返回第一个表达式匹配的结果
- * matcher.group(2): 返回第二个表达式匹配的结果
- */
- public class Test {
- public static void main(String[] args) {
- String string = "This is 1949, i am 18 years old.";
-
- Pattern pattern = Pattern.compile("This is (.*?), i am (.*?) years");
- Matcher matcher = pattern.matcher(string);
-
- while (matcher.find()) {
- System.out.println(matcher.group());
- System.out.println(matcher.group(0));
- System.out.println(matcher.group(1));
- System.out.println(matcher.group(2));
- }
- }
- }

3.X 示例:提取分组匹配的数据(每组有多个数据)
- import java.util.ArrayList;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * 分组匹配结果提取
- * matcher.group():整个正则表达式匹配的结果
- * matcher.group(0):返回整个正则表达式匹配的结果
- * matcher.group(1):返回第一个表达式匹配的结果
- * matcher.group(2): 返回第二个表达式匹配的结果
- */
- public class Test {
- public static void main(String[] args) {
- String string = "This is 1949, i am 18 years old. This is 1950, i am 19 years old.";
-
- ArrayList<String> arrayList1 = new ArrayList<String>();
- ArrayList<String> arrayList2 = new ArrayList<String>();
-
- Pattern pattern = Pattern.compile("This is (.*?), i am (.*?) years");
- Matcher matcher = pattern.matcher(string);
-
- while (matcher.find()) {
- arrayList1.add(matcher.group(1));
- arrayList2.add(matcher.group(2));
- }
-
- for (int i = 0; i < arrayList1.size(); i++) {
- System.out.println(i + ": " + arrayList1.get(i));
- }
-
- for (int i = 0; i < arrayList2.size(); i++) {
- System.out.println(i + ": " + arrayList2.get(i));
- }
- }
- }

3.X 示例:\s匹配一个空格或一个Tab
- public class Test {
- public static void main(String[] args) {
- // /s 匹配一个空格或一个Tab
- String string1 = "1 2";
- System.out.println(string1.matches("1\\s2"));
-
- String string2 = "1\t2";
- System.out.println(string2);
- System.out.println(string2.matches("1\\s2"));
-
- String string3 = "1\\t2";
- System.out.println(string3);
- System.out.println(string3.matches("1\\s2"));
- }
- }
3.X 示例:* + ? 和 *? +? ??的区别
- * :代表0到无穷大次。默认是贪婪模式,取无穷大;如果后面跟个?,非贪婪模式,取0。
- + :代表1到无穷大次。默认是贪婪模式,取无穷大;如果后面跟个?,非贪婪模式,取1。
- ? :代表0到1。默认是贪婪模式,取无穷大;如果后面跟个?,非贪婪模式,取0。
- *?:代表0到无穷大次。默认是贪婪模式,取无穷大;如果后面跟个?,非贪婪模式,取0。
- +?:代表1到无穷大次。默认是贪婪模式,取无穷大;如果后面跟个?,非贪婪模式,取1。
- ??:代表0到1。默认是贪婪模式,取无穷大;如果后面跟个?,非贪婪模式,取0。
3.X 示例: (AB|CD) 和 AB|CD 区别
- public class Test {
- public static void main(String[] args) {
- // (AB|CD) 和 AB|CD 区别
- System.out.println("learn java".matches("learn\\s(java|php)"));
- System.out.println("learn php".matches("learn\\s(java|php)"));
- System.out.println("learn java".matches("learn\\sjava|php"));
- System.out.println("learn php".matches("learn\\sjava|php"));
- }
- }
3.x 示例:DOTALL
Dot doesn't matches line breaks
备注:.不可以匹配换行符,也就是单行模式,这也是默认模式。
备注:因为是单行模式,<dd><a href="\d{7,8}.html">(.*?)</a>的</a>只有一个,所以,.*和.*?匹配结果一致。
Dot matches line breaks
备注:.可以匹配换行符。
备注:因为.可以匹配换行符,.*又是贪婪模式,所以<dd><a href="\d{7,8}.html">(.*?)</a>会一直匹配到最后一个</a>。.*?是非贪婪模式,所以<dd><a href="\d{7,8}.html">(.*?)</a>只会匹配到第一个</a>。
3.x 示例:DOTALL
4 参考资料
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。