Java Regular Expressions filter letters, numbers, and Chinese-regular expressions

Source: Internet
Author: User

1, Java filter out letters, numbers and Chinese regular expressions

(1) filter out the regular expression of the letter

[^ (a-za-z)]

(2) Regular expressions that filter out numbers

[^ (0-9)]

(3) Filtering out regular expressions in Chinese

[^ (\\U4E00-\\U9FA5)]

(4) Regular expressions that filter out letters, numbers, and Chinese

[^ (A-ZA-Z0-9\\U4E00-\\U9FA5)]

2. Example source code

* * @Title: Filterstr.java * @Package: Com.you.dao * @Description: Filtering numbers, letters and Chinese in Java @Author: Zhanghaidong * @date: March 12, 2014

Afternoon 7:18:20 * @Version V1.2.3 * * Package Com.you.dao; /** * @ class Name: Filterstr * @ Description: Regular expression filter numbers, letters and Chinese * @Author: Zhanghaidong * @date: March 12, 2014 PM 7:18:20/public class Filterstr {/ 
 * * * @Title: Filternumber * @Type: Filterstr * @date: March 12, 2014 afternoon 7:23:03 * @Description: Filter out numbers * @param str
 * @return */public static string Filternumber (string number) {Number = Number.replaceall ("[^ (0-9)]", "");
 return number; /** * * @Title: Filteralphabet * @Type: Filterstr * @date: March 12, 2014 afternoon 7:28:54 * @Description: Filter out letters * @p
 Aram Alph * @return/public static string Filteralphabet (String alph) {Alph = Alph.replaceall ("[^ (a-za-z)]", "");
 return Alph; /** * * @Title: Filterchinese * @Type: Filterstr * @date: March 12, 2014 afternoon 9:12:37 * @Description: Filter out Chinese * @pa Ram Chin * @return/public static string Filterchinese (stringChin) {chin = Chin.replaceall ("[^ (\\U4E00-\\U9FA5)]", "");
 return chin; /** * * @Title: Filter * @Type: FILTERSTR * @date: March 12, 2014 afternoon 9:17:22 * @Description: Filter out letters, numbers and Chinese * @par AM character * @return * */public static String filter (String character) {character = Character.replaceall ("[^ (a-za-z
 0-9\\U4E00-\\U9FA5)] "", "");
 return character; /** * @Title: Main * @Type: FILTERSTR * @date: March 12, 2014 afternoon 7:18:22 * @Description: * @param args * * Publ
 IC static void Main (string[] args) {/** * declaration string you/String you = "^&^&^you123$%$% Hello";
 /** * Call to filter out the number of methods * * you = Filternumber (for you);
 
 /** * Print Results/System.out.println ("filter out the number:" + you); /** * Declaration string hai/String hai = "¥% ...
 4556ahihdjsadhj$%$%, how are you, Wewewe? "
 /** * Call the method of filtering out the letter * * hai = Filteralphabet (hai);
 
 /** * Print Result * * SYSTEM.OUT.PRINTLN ("filter out the letter:" + hai);
 /** * Declaration string dong/String dong = "$%$%$ John 34584yuojk Dick @#¥#%%¥......%&"; /** * Call to filter out the Chinese method * * dong = fIlterchinese (Dong);
 
 /** * Print Results * * SYSTEM.OUT.PRINTLN ("Filtered out Chinese:" + dong);
 /** * Declaration string str/String str = "$%$%$ John 34584yuojk Dick @#¥#%%¥......%&";
 /** * Calls to filter out letters, numbers, and Chinese methods/str = filter (str);
 
 /** * Print Results/System.out.println ("filter out letters, numbers and Chinese:" + str);

 }

}

3, the results of the operation of the instance

Filtered out Number: 123
Filter out letters: Ahihdjsadhjwewewe
Filter out Chinese: dick
Filter out letters, numbers and Chinese: John 34584yuojk Dick

Ps:java Regular expression Filter Chinese characters

String str = "Hello how are you, I'm good thank you"; 
String reg = "[\u2e80-\u9fff]"; 
Pattern Pat = Pattern.compile (reg); 
Matcher mat = Pat.matcher (str); 
String repickstr = Mat.replaceall ("");
System.out.println ("After filtering Chinese:" +repickstr);
Demo:import Java.util.regex.Matcher;
Import Java.util.regex.Pattern; public class T {/** * filter letter * @param alphabet * @return/public static string Filteralphabet (String alphabet) {RE
 Turn Alphabet.replaceall ("[A-za-z]", ""); /** * Filter Number * @param digital * @return/public static string Filterdigital (String digital) {return DIGITAL.REPLA
 Ceall ("[0-9]", ""); /** * Filter Kanji * @param chin * @return/public static string Filterchinese (String chin) {return chin.replaceall ("[\
 \U4E00-\\U9FA5] "," ""); /** * Filter letters, numbers, Kanji * @param character * @return/public static String Filterall (String character) {return Charac
 Ter.replaceall ("[A-za-z0-9\\u4e00-\\u9fa5]", ""); }/** * @param args */public static void main (string[] args) {//TODO Auto-generated method stub String str = "Hello how are you, I'm good thank you"; 
 String reg = "[\u2e80-\u9fff]"; 
 Pattern Pat = Pattern.compile (reg); 
 Matcher mat = Pat.matcher (str); 
 String repickstr = Mat.replaceall (""); 
 System.out.println ("After filtering Chinese:" +repickstr);
 System.out.println ("-----------------");
 System.out.println (Filteralphabet ("123abc hello"));
 System.out.println (filterdigital ("123abc hello"));
 System.out.println (Filterchinese ("123abc hello"));
 System.out.println (Filterall ("123abc hello")); }
}

The above is about Java regular Expressions filter Chinese, letters, numbers, all the narration, I hope you like.

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.