Home > Java > Javagetting Started > Some understandings about java regular rules?:,?=,?!

Some understandings about java regular rules?:,?=,?!

王林
Release: 2020-01-04 17:20:31
forward
2866 people have browsed it

Some understandings about java regular rules?:,?=,?!

官方文档如图:

Some understandings about java regular rules?:,?=,?!

上图是官方文档的介绍,总结一下讲了两个知识点:

(学习视频推荐:java视频教程

1、是否获取匹配并保存匹配的值;

2、正向预查和反向预查。

一、解释是否获取匹配并保存匹配的值

1、()表示捕获分组,获取匹配,()把每个分组里的匹配的值保存起来;

2、(?:)表示非捕获分组,获取匹配,非捕获分组匹配的值不会保存起来,可以提高程序执行速度;

3、(?=?!?

举例说明:

// 是否获取匹配测试文本 abxoxcd

  ..(xox)..       匹配 abxoxcd

  ..(?:xox)..     匹配 abxoxcd

  ..(?=xox)..     匹配 abxo

获取匹配,一般匹配到了字符之后,该字符就被消耗、输出。不获取匹配,不会消耗字符。

// 是否保存匹配的值测试文本 AAABBCaaA E666FF

  (A)\\1*       匹配 AAA、A

  (\\w)\\1+     匹配 AAA、BB、aa、666、FF

  (?:A)(B)\\1*  匹配 ABB
Copy after login

因为非捕获分组不保存匹配的值,所以 (?:A)(B)\\1* 匹配 ABB 第一个\\1表示(B)这个捕获组捕获到的值。

“注意:反向引用表示捕获到的值,而不是再次使用正则表达式”

二、解释正向预查和反向预查

// 前瞻:
exp1(?=exp2)   查找exp2前面的exp1
// 后顾:
(?<=exp2)exp1  查找exp2后面的exp1
// 负前瞻:
exp1(?!exp2)   查找后面不是exp2的exp1
// 负后顾:
(?<!exp2)exp1  查找前面不是exp2的exp1
Copy after login

举例说明:

// 正向预查和反向预查
测试文本 abxoxcd

  ..(?=xox)  匹配 ab

  ..(?=oxo)  匹配不到

  (?=xox)..  匹配xo

  (?<=xox).. 匹配 cd
Copy after login

预查只需要记住:

1、环顾全局定位到非捕获组;

2、不获取匹配(不消耗字符),不保存匹配的值(不能使用反向引用)。

相关文章教程推荐:java快速入门

The above is the detailed content of Some understandings about java regular rules?:,?=,?!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template