Java Test Regular expression (i)

Source: Internet
Author: User

This article mainly tests the usage of the character class, and the result of the test is placed behind the test method as a comment.

Package Regularexpression;import Java.util.regex.matcher;import Java.util.regex.pattern;import org.junit.Test;/** * Test character class; * @author Yuncong * */public class Testregularexpression {/** * 0. A single character matches itself */@Testpublic void test0 () {String reg ex = "D"; String input = "1b2s-[[email protected]^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * d *//** * 1. [C1c2 ...], which means any c1,c2,... One of the characters, * where CI can be multiple characters (such as AB), a character range (such as a-Z, * A-Z, 0-9) or a character class (such as [A-z], \d) */@Testpublic void test1 () {//multiple characters string regex = "[ABCD ]"; String input = "1B2S23D3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * d */@Testpublic void Test2 () {//character range String regex = "[A-za-z]"; String input = "1B2S23D3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = Pattern.matCher (input); while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * B * S * d */@Testpublic void Test3 () {//character class String regex = "[[a-za-z][0-9]]"; String input = "1B2S23D3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 1 * B * 2 * S * 2 * 3 * d * 3 *//** * 2. [^ ...], which represents a character in the complement of a character class */@Testpublic void test4 () {String regex = "[^a-za-z]"; String input = "1B2S23D3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 1 * 2 * 2 * 3 * 3 *//** * If the character class contains ^,^ can be placed anywhere except the starting position, * that is, only if the ^ is placed at the start position, the function of the complement */@Testpublic void Test5 () { The second ^ is a generic match character string regex = "[^0-9^]"; String input = "1b2s2^[email protected]^3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input); WHIle (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * B * S * d * @ *//** * If the character class contains-,-must be the first or last item */@Testpublic void Test6 () {String regex = "[-0-9]"; String input = "[email protected]^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 2 *-* 2 * 3 * 3 *//** * If the character class contains [, must be placed in the first item and escaped */@Testpublic void Test7 () {String regex = "[\\[0-9]"; String input = "1b2s-[[email protected]^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 1 * 2 * [* 2 * 3 * 3 *//** * 3. [... &&], intersection of two character classes */@Testpublic void Test8 () {String regex = "[Abcd&&a-z]"; String input = "1b2s-[[email protected]^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input); (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * d *//** * 4. \w, a Word character class (that is, a character that may exist in a word), * is equivalent to [a-za-z0-9_]/@Testpublic void Test9 () {String regex = "\\w"; String input = "1b2s-[23d li @3^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 1 * B * 2 * S * 2 * 3 * d * 3 *//** * 5. \p{name}, which represents a named character class */@Testpublic void test10 () {String regex = "\\p{upper}"; String input = "1b2s-[23d li @3^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * B */}


Java Test Regular expression (i)

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.