在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. MySQL数据库基本用法

    远程连接数据库 mysql -u root -p #-u 用户名 -h后面写要连接的主机ip地址 -u后面写连接的用户名 -p回车后写密码 回车后输入密码,当前设置的密码为toor 数据库操作 创建数 ...

  2. 【随笔】Android应用市场搜索优化(ASO)

    参考了几篇网上的关于Android应用市场搜索优化(ASO)的分析,总结几点如下: 优化关键字:举例目前美团酒店在各Android市场上的关键字有“美团酒店.钟点房.团购.7天”等等,而酒店类竞对在“ ...

  3. python 列表返回重复数据的下标

    class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...

  4. 多个 gradle 文件夹 \.gradle\wrapper\dists\ 设置gradle不是每次都下载

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 设置gradle不是每次都下载 \.gradle\wrapper\dists\ ==== ...

  5. BZOJ.4753.[JSOI2016]最佳团体(01分数规划 树形背包DP)

    题目链接 \(Description\) 每个点有费用si与价值pi,要求选一些带根的连通块,总大小为k,使得 \(\frac{∑pi}{∑si}\) 最大 \(Solution\) 01分数规划,然 ...

  6. 闪烁的LED灯

    /* Main.c file generated by New Project wizard * * Created: 周五 五月 5 2017 * Processor: 80C31 * Compil ...

  7. [TYVJ1473]校门外的树3

    思路: 维护两个树状数组,一个记录种树区间左端点,一个记录右端点. 每次询问查询“看不见的树区间”,即右端点小于查询区间左端点和左端点小于查询区间右端点. #include<cstdio> ...

  8. c/c++中int main(int argc,char *argv[])的具体含义

    int main(int argc,char * argv[ ]) argv为指针的指针 argc为整数 char **argv or: char *argv[ ] or: char argv[ ][ ...

  9. Linux 标准目录结构 FHS

    因为利用 Linux 来开发产品或 distribution 的团队实在太多了,如果每个人都用自己的想法来配置文件放置的目录,那么将可能造成很多管理上的困扰.所以,后来就有了 Filesystem H ...

  10. PHP Redis 对象方法手册

    redis(Remote Dictionary Server)是一种Nosql技术,它是一个开源的高级kv存储和数据结构存储系统. redis不仅仅是能够存储key和value这种简单的键值对,还能存 ...