在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. 数据恢复工具PhotoRec

    数据恢复工具PhotoRec PhotoRec是一款文件恢复工具.它可以从硬盘.光驱.记忆卡中恢复视频.文档.压缩包等文件.该工具绕开文件系统,采用文件特征码机制,直接进行底层数据扫描,尝试恢复文件. ...

  2. Codeforces Round #408 (Div. 2) 题解【ABCDE】

    A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...

  3. mongodb通过profile来监控数据

    mongodb可以通过profile来监控数据,进行优化.查看当前是否开启profile功能用命令db.getProfilingLevel() 返回level等级,值为0|1|2,分别代表意思:0代表 ...

  4. __getattr__和__setattt__使用

    # coding:utf-8 """ __setattr__(self, name, value),如果要给name赋值,调用此方法 __getattr__(self, ...

  5. File构建实例的路径:绝对路径和相对路径

    public static void main(String[] args) throws Exception { File file = new File("bin/dyan.txt&qu ...

  6. Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko

    Trident (又称为MSHTML),是微软的窗口操作系统(Windows)搭载的网页浏览器—Internet Explorer的排版引擎的名称. 它的第一个版本随着1997年10月Internet ...

  7. Android批量图片加载经典系列——使用xutil框架缓存、异步加载网络图片

    一.问题描述 为提高图片加载的效率,需要对图片的采用缓存和异步加载策略,编码相对比较复杂,实际上有一些优秀的框架提供了解决方案,比如近期在git上比较活跃的xutil框架 Xutil框架提供了四大模块 ...

  8. SpringBootWEB项目和非Web项目的全局异常捕获

    一.简介 SpringBoot的WEB异常捕获,如果是WEB项目的话,可以直接处理Controller中的异常.如果不是WEB项目的话,就需要使用AspectJ来做切面. 二.WEB项目 packag ...

  9. UICollectionView在初始化的时候移动到某个距离

    #pragma mark  -- 使用场景:选中非第一张图片用CollectionView进行浏览时,CollectionView滑动到对应的位置 #pragma mark  -- 重点在于UICol ...

  10. MYSQL 中query_cache_size小结

    1 原理    MySQL查询缓存保存查询返回的完整结果.当查询命中该缓存,会立刻返回结果,跳过了解析,优化和执行阶段. 查询缓存会跟踪查询中涉及的每个表,如果这写表发生变化,那么和这个表相关的所有缓 ...