Java Pattern 翻译

本文详细介绍了Java中正则表达式的使用方法,包括如何将字符串形式的正则表达式编译为Pattern实例,以及如何利用Pattern创建Matcher对象进行匹配操作。此外,还提供了典型调用序列的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.util.regex

Class Pattern

java.lang.Object
  java.util.regex.Pattern
All Implemented Interfaces:
Serializable

public final class Pattern
extends Object implements 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 a Matcher 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. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值