Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Java regular expression capture group

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand the Java regular expression capture group". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the Java regular expression capture group".

Undefined

General capture group (Expression)

Name the capture group (? Expression)

Ordinary capture group

Starting on the left side of the regular expression, each left parenthesis "(") is marked as a grouping, and the grouping number starts at 1. 0 represents the entire expression.

For the time string: 2017-04-25, the expression is as follows

(\ d {4})-(\ d {2})-(\ d {2}))

There are four left parentheses, so there are four groups:

The number capture group matches 0 (\ d {4})-(\ d {2})-(\ d {2}) 2017-04-251 (\ d {4}) 20172 ((\ d {2})-(\ d {2})) 04-253 (\ d {2}) 044 (\ d {2}) 25public static final String DATE_STRING = "2017-04-25" Public static final String P_COMM = "(\\ d {4})-(\\ d {2})-(\\ d {2})"; Pattern pattern = Pattern.compile (P_COMM); Matcher matcher = pattern.matcher (DATE_STRING); matcher.find (); / / this System.out.printf ("\ nmatcher.group (0) value:%s", matcher.group (0) is required) System.out.printf ("\ nmatcher.group (1) value:%s", matcher.group (1)); System.out.printf ("\ nmatcher.group (2) value:%s", matcher.group (2)); System.out.printf ("\ nmatcher.group (3) value:%s", matcher.group (3)); System.out.printf ("\ nmatcher.group (4) value:%s", matcher.group (4))

Name the capture group

Each capture group that begins with the left parenthesis is followed by a regular expression.

For the time string: 2017-04-25, the expression is as follows:

(?\ d {4})-(?)

There are four named capture groups, namely:

Number name capture group match 00 (?\ d {4})-(?\ d {2})-(?\ d {2}) 2017-04-251year (?\ d {4})-20172md (? (?\ d {2})-(?\ d {2}) 04-253month (?\ d {2}) 044date (?\ d {2}) 25public static final String P_NAMED = "(?\ d {4})-(? {2})-(?\ d {2}) " Public static final String DATE_STRING = "2017-04-25"; Pattern pattern = Pattern.compile (P_NAMED); Matcher matcher = pattern.matcher (DATE_STRING); matcher.find (); System.out.printf ("\ n = get by name ="); System.out.printf ("\ nmatcher.group (0) value:%s", matcher.group (0)); System.out.printf ("\ nmatcher.group ('year') value:%s", matcher.group ("year")) System.out.printf ("\ nmatcher.group ('md') value:%s", matcher.group ("md")); System.out.printf ("\ nmatcher.group (' month') value:%s", matcher.group ("month")); System.out.printf ("\ nmatcher.group ('date') value:%s", matcher.group ("date")); matcher.reset (); System.out.printf ("\ n = get by number ="); matcher.find () System.out.printf ("\ nmatcher.group (0) value:%s", matcher.group (0)); System.out.printf ("\ nmatcher.group (1) value:%s", matcher.group (1)); System.out.printf ("\ nmatcher.group (2) value:%s", matcher.group (2)); System.out.printf ("\ nmatcher.group (3) value:%s", matcher.group (3)) System.out.printf ("\ nmatcher.group (4) value:%s", matcher.group (4))

PS: non-capture group

The left parenthesis is followed by?:, followed by a regular expression to form a non-capture group (?: Expression).

For the time string: 2017-04-25, the expression is as follows:

(?:\ d {4})-(\ d {2})-(\ d {2}))

Although this regular expression has four left parentheses, there are theoretically four capture groups. But the first group (?:\ d {4}) is actually ignored. When using matcher.group (4), the system will report an error.

The number capture group matches 0 (\ d {4})-(\ d {2})-(\ d {2})) 2017-04-251 ((\ d {2})-(\ d {2})) 04-252 (\ d {2}) 043 (\ d {2}) 25 Thank you for your reading. This is the content of "how to understand the Java regular expression capture group". After the study of this article, I believe you have a deeper understanding of how to understand the Java regular expression capture group, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report