Java test Regular expression (ii)

Source: Internet
Author: User

This article mainly tests the use of quantifiers and quantifier suffixes, and the results of the tests are placed behind the test methods as comments.

Package Regularexpression;import Java.util.regex.matcher;import Java.util.regex.pattern;import org.junit.Test;/** * Test quantifiers and quantifier suffixes; * x and Y are regular expressions * @author Yuncong * */public class TestRegularExpression2 {/** * xy means matches of any x followed by Match of Y */@Testpublic V OID Test0 () {String regex = "DC"; String input = "1bdc2d"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * DC *//** * x| Y represents any x or y match */@Testpublic void test1 () {String regex = "D|c"; String input = "1bdc2d"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * d * c * d *//** * x+ denotes 1 or more x; * By default, * matches as many x */@Testpublic void Test2 () {String regex = "D" If the match is guaranteed to be successful) [A-z]+d]; String input = "1DDCCBDCCCCD2DCD"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input); while (Matcher. Find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}}  /** * Output: * DCCCCD * DCD *//** * x* represents 0 or more x; * By default, * matches as many x */@Testpublic void Test3 () {String regex = as possible if the match is guaranteed to be successful) "D[a-z]*d"; String input = "1DDCCBDCCCCD2DCD"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * DD * DCCCCD * DCD *//** * x = 0 or 1 x; * By default, * matches as many x */@Testpublic void test4 () {String re) as possible if the match is guaranteed to succeed Gex = "D[a-z]?d"; String input = "1DDCCBDCCCCD2DCD"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * DD * DCD *//** * quantifier suffix? , * match the number of X (? is the suffix of the quantifier of X), if the match is guaranteed to be successful; * Same as Default */@Testpublic void Test5 () {String regex = "D[a-z]*?d"; String input = "1DDCCBDCCCCD2DCD"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input); while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * DD * DCCCCD * DCD *//** * quantifier suffix +, * match as many X (? is the suffix of the quantifier of X), even if the match fails; */@Testpublic void Test6 () {String regex = "D[a-z]*+d"; String input = "1DDCCBDCCCCD2DCD"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * (no match to any string) */}


Java test Regular expression (ii)

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.