Java Regular Expressions filter out letters, numbers, and Chinese

Source: Internet
Author: User

Original: http://blog.csdn.net/k21325/article/details/54090066

1. Regular expressions in Java that filter out letters, numbers, and Chinese

(1) Regular expressions to filter out letters

[HTML]View PlainCopy
    1. [^ (a-za-z)]

(2) Regular expressions for filtering out numbers

[HTML]View PlainCopy
    1. [^ (0-9)]

(3) Regular expressions to filter out Chinese

[HTML]View PlainCopy
    1. [^ (\\U4E00-\\U9FA5)]

(4) Filter out regular expressions of letters, numbers and Chinese

[HTML]View PlainCopy
    1. [^ (A-ZA-Z0-9\\U4E00-\\U9FA5)]

2. Example source code

[Java]View PlainCopy
  1. **
  2. * @Title: Filterstr.java
  3. * @Package: Com.you.dao
  4. * @Description: Filter numbers, letters and Chinese in Java
  5. * @Author: Zhanghaidong
  6. * @date: 2014 March 12th pm 7:
  7. * @Version V1. 2.3
  8. */
  9. Package Com.you.dao;
  10. /**
  11. * @ Class Name: Filterstr
  12. * Description: Regular Expressions filter numbers, letters, and Chinese
  13. * @Author: Zhanghaidong
  14. * @date: March 12, 2014 7:18:20
  15. */
  16. Public class Filterstr
  17. {
  18. /** 
  19. *
  20. * @Title: Filternumber
  21. * @Type: Filterstr
  22. * @date: March 12, 2014 7:23:03
  23. * @Description: Filter out numbers
  24. * @param str
  25. * @return
  26. */
  27. public static string Filternumber (string number)
  28. {
  29. Number = Number.replaceall ("[^ (0-9)]", " ");
  30. return number;
  31. }
  32. /** 
  33. *
  34. * @Title: Filteralphabet
  35. * @Type: Filterstr
  36. * @date: March 12, 2014 7:28:54
  37. * @Description: Filter out letters
  38. * @param Alph
  39. * @return
  40. */
  41. public static string Filteralphabet (String Alph)
  42. {
  43. Alph = Alph.replaceall ("[^ (a-za-z)]", " ");
  44. return Alph;
  45. }
  46. /** 
  47. *
  48. * @Title: Filterchinese
  49. * @Type: Filterstr
  50. * @date: March 12, 2014 9:12:37
  51. * @Description: Filter out Chinese
  52. * @param Chin
  53. * @return
  54. */
  55. public static string Filterchinese (String chin)
  56. {
  57. Chin = Chin.replaceall ("[^ (\\U4E00-\\U9FA5)]", " ");
  58. return chin;
  59. }
  60. /** 
  61. *
  62. * @Title: Filter
  63. * @Type: Filterstr
  64. * @date: March 12, 2014 9:17:22
  65. * @Description: Filter out letters, numbers and Chinese
  66. * @param character
  67. * @return
  68. */
  69. public static string filter (string character)
  70. {
  71. character = Character.replaceall ("[^ (A-ZA-Z0-9\\U4E00-\\U9FA5)]", " ");
  72. return character;
  73. }
  74. /** 
  75. * @Title: Main
  76. * @Type: Filterstr
  77. * @date: March 12, 2014 7:18:22
  78. * @Description:
  79. * @param args
  80. */
  81. public static void Main (string[] args)
  82. {
  83. /** 
  84. * Declaration String you
  85. */
  86. String you = "^&^&^you123$%$% Hello";
  87. /** 
  88. * Call a method to filter out numbers
  89. */
  90. you = Filternumber (you);
  91. /** 
  92. * Print Results
  93. */
  94. System.out.println ("Filter out Numbers:" + you);
  95. /** 
  96. * Declaration string Hai
  97. */
  98. String hai = "¥% ...  4556ahihdjsadhj$%$% How are you Wewewe ";
  99. /** 
  100. * Call a method to filter out letters
  101. */
  102. hai = Filteralphabet (hai);
  103. /** 
  104. * Print Results
  105. */
  106. System.out.println ("filter out the letter:" + hai);
  107. /** 
  108. * Declaration String Dong
  109. */
  110. String dong = "$%$%$ Zhang San 34584yuojk John Doe @#¥#%%¥......%&";
  111. /** 
  112. * Call to filter out the Chinese method
  113. */
  114. Dong = Filterchinese (dong);
  115. /** 
  116. * Print Results
  117. */
  118. System.out.println ("Filter out Chinese:" + dong);
  119. /** 
  120. * Declaration String str
  121. */
  122. String str = "$%$%$ Zhang San 34584yuojk John Doe @#¥#%%¥......%&";
  123. /** 
  124. * Call methods to filter out letters, numbers and Chinese
  125. */
  126. str = filter (str);
  127. /** 
  128. * Print Results
  129. */
  130. System.out.println ("filter out letters, numbers and Chinese:" + str);
  131. }
  132. }

3. Example running result

Filter out numbers: 123
Filter out letters: Ahihdjsadhjwewewe
Filter out Chinese: Dick and Harry
Filter out letters, numbers and Chinese: Zhang San 34584yuojk John Doe

Ps:java Regular expression filtering Chinese characters

[Java]View PlainCopy
    1. String str = "Hello Hi, I'm good thank you";
    2. String reg = "[\u2e80-\u9fff]";
    3. Pattern Pat = Pattern.compile (reg);
    4. Matcher mat = Pat.matcher (str);
    5. String repickstr = Mat.replaceall ("");
    6. System.out.println ("After filtering Chinese:" +repickstr);

[Java]View PlainCopy
  1. Import Java.util.regex.Matcher;
  2. Import Java.util.regex.Pattern;
  3. Public class T {
  4. /** 
  5. * Filter Letters
  6. * @param alphabet
  7. * @return
  8. */
  9. public static string Filteralphabet (String alphabet) {
  10. return Alphabet.replaceall ("[A-za-z]", " ");
  11. }
  12. /** 
  13. * Filter Numbers
  14. * @param Digital
  15. * @return
  16. */
  17. public static string Filterdigital (String digital) {
  18. return Digital.replaceall ("[0-9]", " ");
  19. }
  20. /** 
  21. * Filter Chinese characters
  22. * @param Chin
  23. * @return
  24. */
  25. public static string Filterchinese (String chin) {
  26. return Chin.replaceall ("[\\u4e00-\\u9fa5]", " ");
  27. }
  28. /** 
  29. * Filter letters, numbers, kanji
  30. * @param character
  31. * @return
  32. */
  33. public static string Filterall (string character) {
  34. return Character.replaceall ("[A-za-z0-9\\u4e00-\\u9fa5]", " ");
  35. }
  36. /** 
  37. * @param args
  38. */
  39. public static void Main (string[] args) {
  40. //TODO auto-generated method stub
  41. String str = "Hello Hi, I'm good thank you";
  42. String reg = "[\u2e80-\u9fff]";
  43. Pattern Pat = Pattern.compile (reg);
  44. Matcher mat = Pat.matcher (str);
  45. String repickstr = Mat.replaceall ("");
  46. System.out.println ("After filtering Chinese:" +repickstr);
  47. System.out.println ("-----------------");
  48. System.out.println (Filteralphabet ("123abc Hello"));
  49. System.out.println (Filterdigital ("123abc Hello"));
  50. System.out.println (Filterchinese ("123abc Hello"));
  51. System.out.println (Filterall ("123abc Hello"));
  52. }
  53. }


The above content is about the Java regular expression filter Chinese, letters, numbers of all the narrative, I hope you like.

Java Regular Expressions filter out letters, numbers, and Chinese

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.