在JDK 1.4中,Java增加了对正则表达式的支持。

java与正则相关的工具主要在java.util.regex包中;此包中主要有两个类:PatternMatcher


Pattern 

声明:public final class Pattern implements java.io.Serializable

Pattern类有final 修饰,可知他不能被子类继承。

含义:模式类,正则表达式的编译表示形式。

注意:此类的实例是不可变的,可供多个并发线程安全使用。


字段:

 public static final int UNIX_LINES = 0x01;

    /**
* 启用不区分大小写的匹配。*/
public static final int CASE_INSENSITIVE = 0x02; /**
* 模式中允许空白和注释。
*/
public static final int COMMENTS = 0x04; /**
* 启用多行模式。
*/
public static final int MULTILINE = 0x08; /**
* 启用模式的字面值解析。*/
public static final int LITERAL = 0x10; /**
* 启用 dotall 模式。
*/
public static final int DOTALL = 0x20; /**
* 启用 Unicode 感知的大小写折叠。*/
public static final int UNICODE_CASE = 0x40; /**
*  启用规范等价。
*/
public static final int CANON_EQ = 0x80;
private static final long serialVersionUID = 5073258162644648461L; /**
* The original regular-expression pattern string.
*/
private String pattern; /**
* The original pattern flags.
*/
private int flags; /**
* Boolean indicating this Pattern is compiled; this is necessary in order
* to lazily compile deserialized Patterns.
*/
private transient volatile boolean compiled = false; /**
* The normalized pattern string.
*/
private transient String normalizedPattern; /**
* The starting point of state machine for the find operation. This allows
* a match to start anywhere in the input.
*/
transient Node root; /**
* The root of object tree for a match operation. The pattern is matched
* at the beginning. This may include a find that uses BnM or a First
* node.
*/
transient Node matchRoot; /**
* Temporary storage used by parsing pattern slice.
*/
transient int[] buffer; /**
* Temporary storage used while parsing group references.
*/
transient GroupHead[] groupNodes; /**
* Temporary null terminated code point array used by pattern compiling.
*/
private transient int[] temp; /**
* The number of capturing groups in this Pattern. Used by matchers to
* allocate storage needed to perform a match.此模式中的捕获组的数目。
*/
transient int capturingGroupCount; /**
* The local variable count used by parsing tree. Used by matchers to
* allocate storage needed to perform a match.
*/
transient int localCount; /**
* Index into the pattern string that keeps track of how much has been
* parsed.
*/
private transient int cursor; /**
* Holds the length of the pattern string.
*/
private transient int patternLength;

组和捕获

捕获组可以通过从左到右计算其开括号来编号。

在表达式 ((A)(B(C))) 中,存在四个组:

1 ABC
2 A
3 BC
4 C

组零始终代表整个表达式。


构造器

    private Pattern(String p, int f) {
pattern = p;
flags = f; // Reset group index count
capturingGroupCount = 1;
localCount = 0; if (pattern.length() > 0) {
compile();
} else {
root = new Start(lastAccept);
matchRoot = lastAccept;
}
}

构造器是私有的,可知不能通过new创建Pattern对象。

如何得到Pattern类的实例?

查阅所有方法后发现:

    public static Pattern compile(String regex) {
return new Pattern(regex, 0);
}
    public static Pattern compile(String regex, int flags) {
return new Pattern(regex, flags);
}

可知是通过Pattern调用静态方法compile返回Pattern实例。

java之Pattern类详解的更多相关文章

  1. java之Matcher类详解

    在JDK 1.4中,Java增加了对正则表达式的支持. java与正则相关的工具主要在java.util.regex包中:此包中主要有两个类:Pattern.Matcher. Matcher  声明: ...

  2. java之StringBuffer类详解

    StringBuffer 线程安全的可变字符序列. StringBuffer源码分析(JDK1.6): public final class StringBuffer extends Abstract ...

  3. java之AbstractStringBuilder类详解

    目录 AbstractStringBuilder类 字段 构造器 方法   public abstract String toString() 扩充容量 void  expandCapacity(in ...

  4. java之StringBuilder类详解

    StringBuilder 非线程安全的可变字符序列 .该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).如果可能,建议优先采用该类,因为在 ...

  5. java.lang.Thread类详解

    java.lang.Thread类详解 一.前言 位于java.lang包下的Thread类是非常重要的线程类,它实现了Runnable接口,今天我们来学习一下Thread类,在学习Thread类之前 ...

  6. Java中dimension类详解

    Java中dimension类详解 https://blog.csdn.net/hrw1234567890/article/details/81217788

  7. java的ReentrantLock类详解

    ReentrantLock 能用于更精细化的加锁的Java类, 通过它能更清楚了解Java的锁机制 ReentrantLock 类的集成关系有点复杂, 既有内部类, 还有多重继承关系 类的定义 pub ...

  8. Java的String类详解

    Java的String类 String类是除了Java的基本类型之外用的最多的类, 甚至用的比基本类型还多. 同样jdk中对Java类也有很多的优化 类的定义 public final class S ...

  9. Java Properties工具类详解

    1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...

随机推荐

  1. NOIp模拟赛 现实(DP 拓扑)

    题目来源:by lzz \(Description\) 给定一张有向图,求对于哪些点,删除它和它的所有连边后,图没有环. \(n\leq 5\times10^5,m\leq 10^6\). \(Sol ...

  2. 洛谷.4114.Qtree1(树链剖分)

    题目链接 模板题都错了这么多次.. //边权赋到点上 树剖模板 //注意LCA.链的顶端不能统计到答案! #include <cstdio> #include <cctype> ...

  3. 探究functools模块wraps装饰器的用途

    <A Byte of Python>17.8节讲decorator的时候,用到了functools模块中的一个装饰器:wraps.因为之前没有接触过这个装饰器,所以特地研究了一下. 何谓“ ...

  4. Python常见下划线

    python中常见的下划线意义 Python中常常使用下划线里对变量进行修饰,通常作为变量的前缀或者后缀出现,被修饰的变量一般存在特殊的用法: _XXX:不能被from module import _ ...

  5. swoole深入学习 4. process

    swoole-1.7.2增加了一个进程管理模块,用来替代PHP的pcntl扩展.pcntl是php新增的一个多进程扩展,用来实现多进程,但是有很多不完善的地方,swoole 就完善了这些地方,而且使得 ...

  6. TCP/IP通信协议

    TCP/IP 是用于 Internet 的通信协议. 一.什么是 TCP/IP 1.TPC/IP 是供已连接 Internet 的计算机进行通信的协议: 2.TCP(Transmission Cont ...

  7. 2d场景背景无限滚动

    之前都是直接借用的DoTween插件,两个背景无限交替位置进行,还有就是三个背景在利用Trigger进行判断显示与否循环: 示例脚本: private List<RectTransform> ...

  8. Linux进程优先级系统——设置实时进程优先级

    前言 最近研发的产品出了点小bug,最后查到根本原因是,其中一个进程A使用基于FIFO的实时进程优先级,而另一个进程B是使用普通调度的进程优先级,而A和B两个进程是互相通信的,进程B会被饿死,而进程A ...

  9. Java多线程:Linux多路复用,Java NIO与Netty简述

    JVM的多路复用器实现原理 Linux 2.5以前:select/poll Linux 2.6以后: epoll Windows: IOCP Free BSD, OS X: kqueue 下面仅讲解L ...

  10. 机器学习笔记(4):多类逻辑回归-使用gluton

    接上一篇机器学习笔记(3):多类逻辑回归继续,这次改用gluton来实现关键处理,原文见这里 ,代码如下: import matplotlib.pyplot as plt import mxnet a ...