Java正则表达式Pattern和Matcher类详解

概述

  • Pattern类的作用在于编译正则表达式后创建一个匹配模式.

  • Matcher类使用Pattern实例提供的模式信息对正则表达式进行匹配

Pattern类

常用方法及介绍

  • Pattern complie(String regex)
    由于Pattern的构造函数是私有的,不可以直接创建,所以通过静态方法compile(String regex)方法来创建,将给定的正则表达式编译并赋予给Pattern类
  • String pattern() 返回正则表达式的字符串形式,其实就是返回Pattern.complile(String regex)的regex参数
<span class="hljs-keyword">String</span> regex = <span class="hljs-string">"\\?|\\*"</span>;
<span class="hljs-keyword">Pattern</span> pattern = <span class="hljs-keyword">Pattern</span>.compile(regex);
<span class="hljs-keyword">String</span> patternStr = pattern.pattern();//&#x8FD4;&#x56DE;\?\*
  • 1
  • 2
  • 3

  • Pattern compile(String regex, int flags) 方法功能和compile(String regex)相同,不过增加了flag参数

  • int flags() 返回当前Pattern的匹配flag参数.

flag参数用来控制正则表达式的匹配行为,可取值范围如下:

Pattern.CANON_EQ 当且仅当两个字符的”正规分解(canonical decomposition)”都完全相同的情况下,才认定匹配.比如用了这个标志之后,表达式”a\u030A”会匹配”?”.默认情况下,不考虑”规范相等性(canonical equivalence)”.

Pattern.CASE_INSENSITIVE(?i) 默认情况下,大小写不明感的匹配只适用于US-ASCII字符集.这个标志能让表达式忽略大小写进行匹配.要想对Unicode字符进行大小不明感的匹 配,只要将UNICODE_CASE与这个标志合起来就行了.

Pattern.COMMENTS(?x) 在这种模式下,匹配时会忽略(正则表达式里的)空格字符(译者注:不是指表达式里的”\s”,而是指表达式里的空格,tab,回车之类).注释从#开始,一直到这行结束.可以通过嵌入式的标志来启用Unix行模式.

Pattern.DOTALL(?s)在这种模式下,表达式’.’可以匹配任意字符,包括表示一行的结束符。默认情况下,表达式’.’不匹配行的结束符.

Pattern.MULTILINE(?m)在这种模式下,’\^’和’$’分别匹配一行的开始和结束.此外,’^’仍然匹配字符串的开始,’$’也匹配字符串的结束.默认情况下,这两个表达式仅仅匹配字符串的开始和结束.

Pattern.UNICODE_CASE(?u) 在这个模式下,如果你还启用了CASE_INSENSITIVE标志,那么它会对Unicode字符进行大小写不明感的匹配.默认情况下,大小写不敏感的匹配只适用于US-ASCII字符集.

Pattern.UNIX_LINES(?d) 在这个模式下,只有’\n’才被认作一行的中止,并且与’.’,’^’,以及’$’进行匹配.

  • Pattern.matcher(CharSequence input) 对指定输入的字符串创建一个Matcher对象
Pattern pattern = Pattern.compile(<span class="hljs-string">"\\?{2}"</span>);
Matcher matcher = pattern.matcher(<span class="hljs-string">"??"</span>);
boolean <span class="hljs-operator">matches</span> = matcher.<span class="hljs-operator">matches</span>();// <span class="hljs-keyword">true</span>
  • 1
  • 2
  • 3

  • String[] split(CharSequence input)
    String[] split(CharSequence input, int limit)

  • String[] split(CharSequence input, int limit)
    功能和String[] split(CharSequence input)相同,增加参数limit目的在于要指定分割的段数
<span class="hljs-built_in">String</span> regex = <span class="hljs-string">"\\?|\\*"</span>;
Pattern pattern = Pattern.compile(regex);
<span class="hljs-built_in">String</span>[] splitStrs = pattern.split(<span class="hljs-string">"123?123*456*456"</span>);
<span class="hljs-built_in">String</span>[] splitStrs2 = pattern.split(<span class="hljs-string">"123?123*456*456"</span>, <span class="hljs-number">2</span>);
  • 1
  • 2
  • 3
  • 4

  • Pattern.quote(String s) 返回给定的字符串的字面量,关于方法的具体信息请参考123

String pattern = Pattern<span class="hljs-preprocessor">.quote</span>(<span class="hljs-string">"1252343% 8 567 hdfg gf^$545"</span>)
System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(<span class="hljs-string">"Pattern is : "</span>+pattern)
  • 1
  • 2

输出信息:

Pattern is : \Q1252343% 8 567 hdfg gf^$545\E

  • matches()方法编译给定的正则表达式并且对输入的字串以该正则表达式为模开展匹配,该方法适合于该正则表达式只会使用一次的情况,也就是只进行一次匹配工作,因为这种情况下并不需要生成一个Matcher实例.
<span class="hljs-keyword">String</span> regex = <span class="hljs-string">"\\?|\\*"</span>;
Pattern pattern = Pattern.compile(regex);
boolean <span class="hljs-operator">matches</span> = pattern.<span class="hljs-operator">matches</span>(regex, <span class="hljs-string">"?"</span>);//&#x8FD4;&#x56DE;<span class="hljs-keyword">true</span>
  • 1
  • 2
  • 3

Matcher类

常用方法及介绍

  • boolean matches() 最常用方法:尝试对整个目标字符展开匹配检测,也就是只有整个目标字符串完全匹配时才返回真值.
Pattern pattern = Pattern<span class="hljs-preprocessor">.compile</span>(<span class="hljs-string">"\\?{2}"</span>)
Matcher matcher = pattern<span class="hljs-preprocessor">.matcher</span>(<span class="hljs-string">"??"</span>)
boolean matches = matcher<span class="hljs-preprocessor">.matches</span>()
System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(matches)
matcher=pattern<span class="hljs-preprocessor">.matcher</span>(<span class="hljs-string">"?"</span>)
matches = matcher<span class="hljs-preprocessor">.matches</span>()
System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(matches)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

  • boolean lookingAt() 对前面的字符串进行匹配,只有匹配到的字符串在最前面才会返回true

Pattern p = Pattern<span class="hljs-preprocessor">.compile</span>(<span class="hljs-string">"\\d+"</span>)
Matcher m = p<span class="hljs-preprocessor">.matcher</span>(<span class="hljs-string">"22bb23"</span>)
boolean match = m<span class="hljs-preprocessor">.lookingAt</span>()
System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(match)
m = p<span class="hljs-preprocessor">.matcher</span>(<span class="hljs-string">"bb2233"</span>)
match= m<span class="hljs-preprocessor">.lookingAt</span>()
System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(match)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

  • boolean find() 对字符串进行匹配,匹配到的字符串可以在任何位置

Pattern p = Pattern<span class="hljs-preprocessor">.compile</span>(<span class="hljs-string">"\\d+"</span>)
Matcher m = p<span class="hljs-preprocessor">.matcher</span>(<span class="hljs-string">"22bb23"</span>)
m<span class="hljs-preprocessor">.find</span>()
Matcher m2 = p<span class="hljs-preprocessor">.matcher</span>(<span class="hljs-string">"aa2223"</span>)
m2<span class="hljs-preprocessor">.find</span>()
Matcher m3 = p<span class="hljs-preprocessor">.matcher</span>(<span class="hljs-string">"aa2223bb"</span>)
m3<span class="hljs-preprocessor">.find</span>()
Matcher m4 = p<span class="hljs-preprocessor">.matcher</span>(<span class="hljs-string">"aabb"</span>)
m4<span class="hljs-preprocessor">.find</span>()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

  • int start() 返回当前匹配到的字符串在原目标字符串中的位置

  • int end() 返回当前匹配的字符串的最后一个字符在原目标字符串中的索引位置.

  • String group() 返回匹配到的子字符串
    Pattern.start(),Pattern.end(),Pattern.group()代码示例

Pattern p = Pattern<span class="hljs-preprocessor">.compile</span>(<span class="hljs-string">"\\d+"</span>)
Matcher m = p<span class="hljs-preprocessor">.matcher</span>(<span class="hljs-string">"aa22bb23"</span>)
m<span class="hljs-preprocessor">.find</span>()
int start = m<span class="hljs-preprocessor">.start</span>()
String group = m<span class="hljs-preprocessor">.group</span>()
int end = m<span class="hljs-preprocessor">.end</span>()
System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(start)
System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(group)
System<span class="hljs-preprocessor">.out</span><span class="hljs-preprocessor">.println</span>(end)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

还有一些其他常用的方法,请参考API自行学习或者参考其他博客.

参考博客:
1. http://www.cnblogs.com/playing/archive/2011/03/15/1984943.html
2. http://blog.csdn.net/cclovett/article/details/12448843
3. http://www.blogjava.net/jayslong/archive/2011/04/21/embeded_flag_expressions_in_java_regex.html

Original: https://www.cnblogs.com/chujian007/p/16312106.html
Author: 初见洞洞拐
Title: Java正则表达式Pattern和Matcher类详解

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/599137/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球