Getting Started regular Expressions (Java)-named captures

Source: Internet
Author: User

Getting Started regular Expressions (Java)-Introduction

Get started regular Expressions (Java)-1-Basics of Getting Started
Get started regular Expressions (Java)-2-Basic instances

Getting Started regular Expressions (Java)-3-Regular usage in Java

Getting Started regular Expressions (Java)-matching principle-1-engine classification and pervasive principles

Getting Started regular Expressions (Java)-matching principle-2-backtracking

Getting Started regular Expressions (Java)-named captures

Many regular engines support named groupings, in whichJava is introduced in Java7, with syntax and. Net-like (. NET allows the same expression to appear in the same name grouping, Java is not allowed).

A named grouping is a good idea, which is to name the group. Here's a quick walkthrough of how to use Java and what to consider.

1. Define a group named name in the regular

(? <name>x)

Here x is for us to match the content, Note that in this name can not be repeated, the name can not start with a number!

2. Reverse referencing what the name group matches to

\k<name>

Note that the reverse reference is for the content that the group matches to, not the group's expression.

3. Replace, the string that is captured in the reference group name

${name}

4. Get the string captured by the name group

Group (String NAME)

Note: You can also use the ordinal to reference a named capture, starting at 1, and 0 as the full matching result of the regular sequence.

The following is an example of using a simple regular to obtain the month and day respectively:

string s = "2015-10-26"; Pattern p = pattern.compile ("(? <year>\\d{4})-(? <month>\\d{2})-(? <day>\\d{2})"); Matcher m = P.matcher (s), if (M.find ()) {System.out.println ("year:" + M.group ("year")); Year System.out.println ("Month:" + m.group ("month")); Month System.out.println ("Day:" + m.group ("Day")); Day System.out.println ("year:" + m.group (1)); The first set of System.out.println ("month:" + m.group (2)); The second group of System.out.println ("Day:" + m.group (3)); The third group of}system.out.println (S.replaceall (? <year>\\d{4})-(? <month>\\d{2})-(? <day>\\d{2}), "${ Day}-${month}-${year} ")); Change the date of the year-month-day format to day-month-year form 

Output results

year:2015month:10day:26year:2015month:10day:2626-10-2015

Please keep the following link

My blog Address

http://su1216.iteye.com/

http://blog.csdn.net/su1216/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Getting Started regular Expressions (Java)-named captures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.