Groovy Regex groups (groovy regular Expression Group)

Source: Internet
Author: User

Let's look at an example of a Java regular expression.

Import Java.util.regex.matcher;import Java.util.regex.pattern;public class Testmatch {public    static void main ( String[] (args) {        pattern pattern = pattern.compile ("g.*");          Matcher Matcher = Pattern.matcher ("Groovy");          System.out.println (Matcher.matches ());}    }

In fact, this code means that groovy is not a match for regular expressions g.*

For a simple judgment is not a match, write so much code. See how groovy is implemented.

Matcher = "Groovy" =~/g.*/

println (Matcher.matches ())

2 lines of code are done.

How does the regular expression group (regex groups) work? Let's look at a groovy example.

Text = "" "
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
"""

HOUR =/10|11|12| [0-9]/
MINUTE =/[0-5][0-9]/
AM_PM =/am| pm/
Time =/($HOUR):($MINUTE) ($AM _pm)/

Matcher = text =~ time

println (matcher[0] = = ["1:30pm", "1", "+", "PM"])//first Match

println (matcher[0][0] = = "5:00pm")//first Match Group in the first match
println (matcher[0][1] = = "1")//second Match Group in the first match (HOUR)
println (matcher[0][2] = = "a")//third Match Group in the first match (MINUTE)
println (matcher[0][3] = = "PM")//fourth Match Group in the first match (AM_PM)

println (matcher[1] = = ["12:00pm", "+", "xx", "PM"])//second Match
println (matcher[1][0] = = "12:00pm")//first Match Group in the second match
println (matcher[1][1] = = "a")//second Match Group in the Second match (HOUR)
println (matcher[1][2] = = "xx")//third Match Group in the second match (MINUTE)
println (matcher[1][3] = = "PM")//fourth Match Group in the second match (AM_PM)

Where time can be thought of as a regular expression group, it has three "members": ($HOUR), ($MINUTE), ($AM _pm)

Note that it is a "team member" in (), otherwise each character is a member. Note that a question matcher.matches () means "whether the entire string matches the regular expression", for this example, although there are 2 matches in text, However, the result of Matcher.matches () is false because it does not match the entire string. This detail allows you to understand the role of the regular expression group.

Let's look at an example:

Matcher = ". Groovyd,aa3" =~/(\.groovy.), (AA.) /
println (Matcher.matches ())
println (Matcher.getcount ())

/(\.groovy.), (AA.) /There are 2 "team members"

Let's look at an example:

Matcher = ". Groovyd,aa3" =~/\.groovy.,aa./
println (Matcher.matches ())

/\.GROOVY.,AA./has 12 members.

Resources:

1. http://groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups

2. Http://groovy.codehaus.org/FAQ+-+RegExp

3. http://developer.51cto.com/art/200906/129179.htm

4. http://docs.oracle.com/javase/7/docs/api/

Groovy Regex groups (groovy regular Expression Group)

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.