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)