在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. EntityFramework Core

    1,安装EF Core 在.csproj中添加一下配置,用于使用dotnet ef 命令 <ItemGroup> <DotNetCliToolReference Include=&q ...

  2. canvas学习-----1px线条模糊问题

    canvas有时候会出现1像素的线条模糊不清且好像更宽的情况,如下图: 这样的线条显然不是我们想要的. 这篇文章的目的就是弄清楚里面的原理,以及解决它. 大家都知道屏幕上最小的显示尺寸就是1像素,虽然 ...

  3. ajax那些事儿

    一.Ajax的定义浏览器与服务器之间,采用HTTP协议通信.用户在浏览器地址栏键入一个网址,或者通过网页表单向服务器提交内容,这时浏览器就会向服务器发出HTTP请求.Ajax全称Asynchronou ...

  4. 探秘Tomcat(一)

    前言:有的时候自己不知道自己是井底之蛙,这并没有什么可怕的,因为你只要蜷缩在方寸之间的井里,无数次的生活轨迹无非最终归结还是一个圆形:但是可怕的是有一天你不得不从井里跳出来生活,需要重新审视井以外的生 ...

  5. CNN做序列标注问题(tensorflow)

    一.搭建简单的CNN做序列标注代码 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt TIME_ST ...

  6. 潭州课堂25班:Ph201805201 WEB 之 JS 第六课 (课堂笔记)

    上节补充方法 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  7. [NOIp2015提高组]信息传递

    OJ题号:洛谷2661 思路:求最小环.DFS+记忆化. #include<cstdio> #include<cstring> #include<algorithm> ...

  8. sf2gis@163.com

    1.下载boost1.52,http://www.boost.org/.解压文件到d:\boost\boost_1_52_0. 2.下载python2.7.3,http://www.python.or ...

  9. C# Dictionary, SortedDictionary, SortedList

    就我个人觉得Dictionary, SortedDictionary, SortedList 这几个类的使用是比较简单的,只要稍微花点时间在网上查找一点资料,然后在阅读以下源码就理解的很清楚了.为什么 ...

  10. ios mac 对照片进行JPEG压缩

    ios mac 对照片进行JPEG压缩 1. 在iOS上可以使用 API UIImageJPEGRepresentation 对照片数据进行JPEG压缩: 我们知道iOS其实是MAC OS 的移植,那 ...