Regular Expressions used to filter out letters, numbers, and Chinese characters in Java

Source: Internet
Author: User

1. Regular Expressions used to filter out letters, numbers, and Chinese characters in Java

(1) filter out regular expressions of letters

[^ (A-Za-z)]


(2) filter out regular expressions of numbers

[^ (0-9)]


(3) filter out regular expressions in Chinese

[^ (\ U4e00-\ u9fa5)]


(4) Regular Expressions used to filter out letters, numbers, and Chinese characters

[^ (A-zA-Z0-9 \ u4e00-\ u9fa5)]


2. instance source code

/*** @ Title: FilterStr. java * @ Package: com. you. dao * @ Description: Filter numbers, letters, and Chinese characters in Java * @ Author: youhaidong * @ date: 7:18:20, January 1, March 12, 2014 * @ Version V1.2.3 */package com. you. dao;/*** @ Class Name: FilterStr * @ Description: Regular Expressions filter numbers, letters, and Chinese characters * @ Author: You Haidong * @ date: march 12, 2014 7:18:20 */public class FilterStr {/***** @ Title: filterNumber * @ Type: FilterStr * @ date: March 12, 2014 7:23:03 * @ Description: filter out the number * @ param str * @ re Turn */public static String filterNumber (String number) {number = number. replaceAll ("[^ (0-9)]", ""); return number;}/***** @ Title: filterAlphabet * @ Type: FilterStr * @ date: march 12, 2014 7:28:54 * @ Description: filter out the letter * @ param alph * @ return */public static String filterAlphabet (String alph) {alph = alph. replaceAll ("[^ (A-Za-z)]", ""); return alph;}/***** @ Title: filterChinese * @ Type: FilterStr * @ Date: March 12, 2014 9:12:37 * @ Description: filter out Chinese characters * @ param chin * @ return */public static String filterChinese (String chin) {chin = chin. replaceAll ("[^ (\ u4e00-\ u9fa5)]", ""); return chin;}/***** @ Title: filter * @ Type: filterStr * @ date: March 12, 2014 9:17:22 * @ Description: filter out letters, numbers, and Chinese characters * @ param character * @ return */public static String filter (String character) {character = character. repl AceAll ("[^ (a-zA-Z0-9 \ u4e00-\ u9fa5)]", ""); return character;}/*** @ Title: main * @ Type: filterStr * @ date: March 12, 2014 7:18:22 * @ Description: * @ param args */public static void main (String [] args) {/*** declare the String you */String you = "^ & ^ you123 $ % Hello "; /*** call the method to filter out numbers */you = filterNumber (you);/*** print the result */System. out. println ("filter out number:" + you);/*** declare the String hai */String hai = "¥ % ...... 4556 ahihdjsadhj $ % we wewewe ";/*** call the method for filtering out letters */hai = filterAlphabet (hai);/*** print the result */System. out. println ("filtered out letters:" + hai ); /*** declare the String dong */String dong = "$ % $ zhangsan 34584yuojk Li Si @ #¥ #%%%¥ ...... % & ";/*** Call the method to filter out Chinese characters */dong = filterChinese (dong);/*** print the result */System. out. println ("filter out Chinese:" + dong ); /*** declare the String str */String str = "$ % $ zhangsan 34584yuojk LI 4 @ # ¥ #%%%¥ ...... % & ";/*** Call the method to filter out letters, numbers, and Chinese characters */str = filter (str);/*** print the result */System. out. println ("filtering out letters, numbers, and Chinese characters:" + str );}}

3. instance running result

Numbers: 123. Letters: ahihdjsadhjwewewe. Chinese: Zhang sanli. 4. letters, numbers, and Chinese: Zhang san34584yuojk Li Si.


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.