public final class Pattern

extends Objectimplements Serializable

A compiled(编译) representation of a regular expression.

A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create aMatcher object that can match(匹配) arbitrary(任意的) character sequences against(依照) the regular expression. All of the state involved in performing(执行) a match resides(驻留) in the matcher(匹配器), so many matchers can share the same pattern.

A typical invocation sequence is thus

 Pattern p = Pattern.compile("a*b");Matcher m = p.matcher("aaaaab");boolean b = m.matches();

A matches method is defined by this class as a convenience(便利) for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement

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

is equivalent(等同于) to the three statements above(以上), though(不过) for repeated matches it is less efficient(效率) since it does not allow the compiled pattern to be reused.

Instances of this class are immutable and are safe for use by multiple concurrent threads. Instances of the Matcher class are not safe for such use.

public final class Matcher

extends Objectimplements MatchResult

An engine that performs match operations on a character sequence by interpreting(解释) a Pattern.

A matcher is created from a pattern by invoking the pattern'smatcher method. Once created, a matcher can be used to perform three different kinds of match operations:

  • The matches method attempts(尝试) to match the entire(整个) input sequence against the pattern.

  • The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.

  • The find method scans(扫描) the input sequence looking for the next subsequence that matches the pattern.

Each of these methods returns a boolean indicating(表示) success or failure(失败). More information about a successful match can be obtained(获得) by querying(查询) the state of the matcher.

A matcher finds matches in a subset of its input called the region. By default, the region(范围) contains all of the matcher's input. The region can be modified via(通过) theregion method and queried via theregionStart and regionEnd methods. The way that the region boundaries interact with some pattern constructs can be changed. SeeuseAnchoringBounds anduseTransparentBounds for more details(详情).

This class also defines methods for replacing matched subsequences with new strings whose contents can, if desired, be computed(计算) from the match result. TheappendReplacement andappendTail methods can be used in tandem(串联) in order to collect (收集)the result into an existing string buffer, or the more convenient replaceAll method can be used to create a string in which every matching subsequence in the input sequence is replaced.

The explicit state of a matcher includes the start and end indices(索引) of the most recent(最近) successful match. It also includes the start and end indices of the input subsequence captured(捕获) by each capturing group in the pattern as well as(以及) a total(总数) count of such subsequences. As a convenience, methods are also provided for returning these captured subsequences in string form.

The explicit state of a matcher is initially(初始) undefined; attempting(尝试) to query(查询) any part of it before a successful match will cause anIllegalStateException to be thrown. The explicit state of a matcher is recomputed(重新计算) by every match operation.

The implicit(隐式) state of a matcher includes the input character sequence as well as the append position, which is initially zero and is updated by theappendReplacement method.

A matcher may be reset explicitly(明确) by invoking itsreset() method or, if a new input sequence is desired, itsreset(CharSequence) method. Resetting a matcher discards(丢弃) its explicit state information and sets the append position to zero.

Instances of this class are not safe for use by multiple concurrent threads.

转载于:https://www.cnblogs.com/hibernate3-example/archive/2012/05/06/2492489.html

java使用Pattern、Matcher调用正则表达式相关推荐

  1. Java通过Pattern类使用正则表达式

    java正则表达式-java.util.regex.Pattern类判断指定数据 代码示例: 1 /** 2 * 判断字符串是否是数字 3 */ 4 @Test 5 public void testI ...

  2. Java Regex Pattern Matcher

    Java的正则表达式使用,说明在代码中,参照了网络上的一些资料,在此记录下,方便自己如果可能也方便下别人. import java.util.regex.Matcher; import java.ut ...

  3. 【Java】正则表达式 Pattern Matcher

    Pattern: 生成一个正则表达式的模板,一般而言,不会用到Pattern的其他方法.步骤: 1.首先调用其静态方法compile,生成一个正则表达式的模板: 2.调用生成的Pattern实例的ma ...

  4. java matcher group_JAVA正则表达式matcher.find()和 matcher.matches()的区别

    1.find()方法是部分匹配,是查找输入串中与模式匹配的子串,如果该匹配的串有组还可以使用group()函数. matches()是全部匹配,是将整个输入串与模式匹配,如果要验证一个输入的数据是否为 ...

  5. java_14正则表达式Pattern,Matcher,Math,Random,System,BigInteger,BigDecimal,Date,SimpleDateFormat,Calendar

    s'd2019.2.16 14:54 天气热 正则表达式: *定义:一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串. *作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个 ...

  6. Java之Pattern和Matcher的作用

    java.util.regex.Pattern类的实例的主要作用是给正则表达式一个匹配模式,因为在java里面正则表达式是一个字符串,字符串的能力是非常有限的,不像javascript那样可以在正则表 ...

  7. java中Pattern和Matcher的使用

    我们的字符串String类中包含了正则的匹配.字符的替换等操作,但是,有的时候String的正则匹配不能够满足要求就需要用到Pattern和Matcher类 import java.util.rege ...

  8. 我爱学Java之Pattern和Matcher用法

    Java正则表达式通过java.util.regex包下的Pattern和Matcher类实现 Pattern类用于创建一个正则表达式,也可以说是创建一个匹配模式,可以通过两个静态方法创建:compi ...

  9. java matcher group_Java正则表达式matcher.group()用法

    java中正则匹配 group是针对()来说的,因为你匹配到的结果是一组,group(0)就是指的整个串,group(1) 指的是第一个括号里的东西,group(2)指的第二个括号里的东西. 代码: ...

  10. Java Pattern.matcher()方法具有什么功能呢?

    转自: Java Pattern.matcher()方法具有什么功能呢? 下文笔者讲述Pattern.matcher()方法的功能简介说明,如下所示: Pattern.matcher()方法的功能:用 ...

最新文章

  1. tensorflow 进阶(三),BP神经网络之两层hidden_layer
  2. 注册oracle驱动,注册设备 ID - 编写适用于 Oracle® Solaris 11.2 的设备驱动程序
  3. debian 文件夹中文件大小_debian 记录用到的命令 文件夹 目录等操作
  4. Hashtable元素的删除
  5. 安装Windows 2003 域控制器
  6. 关于Kafka高性能的几个问题
  7. 广东哪所大学计算机专业好,准备考研,广东哪所大学的计算机专业最好?除了985,这所大学性价比很高...
  8. 为什么“how to say”是错的?
  9. python 语句执行顺序_一个针对 Python 语句执行顺序的练习
  10. 开发技术理论学习与实践的关系
  11. Android深度探索(卷1)HAL与驱动开发学习笔记(8)
  12. JavaWeb——mybatis模糊查询与主键返回
  13. 内存不稳定导致的故障
  14. hdfs文件系统无法在网页上显示
  15. 我的linux学习之路---配置VNC服务器(1)
  16. 安卓为什么卡及其解决方案
  17. 课改 计算机 论文,计算机论文 计算机应用课改分析
  18. pip慢?半分钟解决方案
  19. 常用 ASCII 码整理
  20. ni visa pci_VISA/MASTER信用卡在线缴费友邦、保诚步骤!

热门文章

  1. Transformer的一家!
  2. 详解中文维基百科数据处理流程及脚本代码
  3. 机器学习基础算法17-决策树-鸢尾花数据集分类及决策树深度与过拟合
  4. 梯度消失与梯度爆炸原因剖析
  5. 博文视点大讲堂第20期——Windows 7来了
  6. 用UCenter Home启动一个垂直的SNS社区
  7. 65 ----点到平面及直线的距离、两异面直线间的距离
  8. PyTorch:卷积/padding/pooling api
  9. linux及windows文件共享
  10. 直方图均衡图像对比度(histogram equalization)PYTHON+OPENCV2