Java String matches Regular expression

Source: Internet
Author: User

  

  1. Package test;
  2. /**
  3. * Use regular expressions in the matches () method of String, Split () method.
  4. * @author fhd001
  5. */
  6. public class Regextest {
  7. public static void Main (string[] args) {
  8. /*
  9. * Normal characters
  10. */
  11. String str1 = "abc45abc345";
  12. STRING[]ARR1 = Str1.split ("abc");
  13. for (String string:arr1) {
  14. System.out.print (string+"--");
  15. }
  16. System.out.println ();
  17. /*
  18. * Simple escape character
  19. * Be sure to use a double backslash when using the literal characters in Java and two slashes to escape into a slash,
  20. * Escaping those special characters with this slash character.
  21. */
  22. String str2 = "^$ () []{}.? +*|";  
  23. Boolean flag = Str2.matches ("//^//$//(//)//[//]//{//}//.//?//+//*//|");
  24. SYSTEM.OUT.PRINTLN (flag);
  25. /*
  26. * Escape character/q.../e.
  27. */
  28. String STR3 = "^$ () []{}.?  +*|99999 ";
  29. Boolean flag2 = Str3.matches ("//q^$ ()" []{}.?  +*|//E//D{5} ");
  30. System.out.println (FLAG2);
  31. /*
  32. * Character Set fit
  33. */
  34. String STR4 = "dfddri334";
  35. Boolean flag4 = Str4.matches (". +");
  36. System.out.println (FLAG4);
  37. String STR5 = "#$%^* ())%";
  38. Boolean flag5 = Str5.matches ("//w{9}");
  39. System.out.println (FLAG5);
  40. String STR6 = "4GFFMDKEKRHHR";
  41. Boolean flag6 = Str6.matches ("//w+");
  42. System.out.println (FLAG6);
  43. String STR7 = "Fjfdke Eett";
  44. Boolean Flag7 = Str7.matches ("//w+//s+//w{4}//s?");
  45. System.out.println (FLAG7);
  46. String str8 = "Erefff";
  47. Boolean Flag8 = Str8.matches ("//s+//s+//s+");
  48. System.out.println (FLAG8);
  49. String STR9 = "456776888";
  50. Boolean flag9 = Str9.matches ("//d+");
  51. System.out.println (FLAG9);
  52. String str10 = "RTYDFGRGWVR";
  53. Boolean flag10 = Str10.matches ("//d+");
  54. System.out.println (FLAG10);
  55. /*
  56. * Custom Character Set collection []
  57. */
  58. String Str11 = "Fdfeetg 34566";
  59. Boolean flag11 = Str11.matches ("[fdetg]+//s+[3-6]+");
  60. System.out.println (FLAG11);
  61. String str12 = "Rtyuie 5768";
  62. Boolean flag12 = Str12.matches ("[^abcdf]+//s+[^1234]+");
  63. System.out.println (FLAG12);
  64. /*
  65. * Match number Qualifier
  66. */
  67. Greedy mode
  68. String Str13 = "ytreggcv454444444333";
  69. Boolean FLAG13 = Str13.matches ("//w{20}");
  70. System.out.println (FLAG13);
  71. Boolean flag14 = Str13.matches ("//w{10,21}");
  72. System.out.println (FLAG14);
  73. Boolean flag15 = Str13.matches ("//w{18,}");
  74. System.out.println (FLAG15);
  75. String str14 = "4";
  76. Boolean flag16 = Str14.matches ("//d?");
  77. System.out.println (FLAG16);
  78. String str15 = "DDCVGT";
  79. Boolean flag17 = Str15.matches ("//d+//d?");
  80. System.out.println (FLAG17);
  81. String str16 = "e33tf44t44t";
  82. Boolean flag18 = Str16.matches ("//w+//w*");
  83. System.out.println (FLAG18);
  84. Reluctant mode (only one example)
  85. String str17 = "34567ghjkkld";
  86. Boolean flag19 = Str17.matches ("//d{2,7}?//w{8,11}");
  87. System.out.println (FLAG19);
  88. Possessive mode (for example only)
  89. String str18 = "22222DDDDD";
  90. Boolean flag20 = Str18.matches ("//d{2,5}+//w{6}");
  91. System.out.println (FLAG20);
  92. /*
  93. * Character boundaries
  94. */
  95. String str19 = "A444545rot44tm";
  96. Boolean flag21 = Str19.matches ("^a//w+m$");
  97. System.out.println (FLAG21);
  98. /*
  99. * Select Expression XXX | Xxx
  100. */
  101. String Str20 = "ABC123ABC";
  102. Boolean flag22 = Str20.matches ("(abc|123) {3}");
  103. System.out.println (FLAG22);
  104. /*
  105. * Group ().
  106. * The above modifiers are decorated for one character. If you want to do a group of characters
  107. * The decoration will be used ()
  108. */
  109. String Str21 = "123qwe123qwe";
  110. Boolean flag23 = Str21.matches ("(123qwe) {2}");
  111. System.out.println (FLAG23);
  112. /*
  113. * [] The intersection and the set
  114. */
  115. String str22 = "abcdefgh1234567";
  116. Boolean flag24 = Str22.matches ("[a-z1-9]+"); and set
  117. System.out.println (FLAG24);
  118. Boolean flag25 = Str22.matches ("[a-z1-9&&[a-h1-7]]+");//Intersection
  119. System.out.println (FLAG25);
  120. }
  121. }

Result code
    1. -45-->345-->
    2. True
    3. True
    4. True
    5. True
    6. True
    7. True
    8. True
    9. True
    10. True
    11. True
    12. True
    13. True
    14. True
    15. True
    16. True
    17. True
    18. True
    19. True
    20. False
    21. True
    22. True
    23. True
    24. True
    25. True

Java String matches Regular expression

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.