Regular expressions matching Chinese characters in java

Source: Internet
Author: User

To match a regular expression with a Chinese character, you must first understand the Chinese character encoding.

The code is as follows: Copy code

[U4E00-u9FA5] Chinese characters? [UFE30-uFFA0] fullwidth characters

[U4E00-u9FA5] Chinese characters? [UFE30-uFFA0] fullwidth characters

Regular Expression Matching Chinese characters: [u4e00-u9fa5]
Match double byte characters (including Chinese characters): [^ x00-xff]

Application: calculate the length of a string (two-byte length meter 2, ASCII character meter 1)

The code is as follows: Copy code
String. prototype. len = function () {return this. replace ([^ x00-xff]/g, "aa"). length ;}

Regular expression for matching empty rows: n [s |] * r

Regular expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/

Regular expression matching the first and last Spaces: (^ s *) | (s * $)


Once we know it, we can solve it.

The code is as follows: Copy code

Public static void regxChinese (){
// String to be matched
String source = "<span title = '5-star hotels 'class = 'dx dx5'> ";
// Convert the string to lowercase
// Source = source. toLowerCase ();
// Regular expression of the matched string
String reg_charset = "<span [^>] *? Title = '([0-9] * [\ s | \ S] * [u4E00-u9FA5] *)' [\ s | \ S]

* Class = '[a-z] * [\ s | \ S] * [a-z] * [0-9] *' ";
  
Pattern p = Pattern. compile (reg_charset );
Matcher m = p. matcher (source );
While (m. find ()){
System. out. println (m. group (1 ));
       } 
}
Public static void regxChinese (){
// String to be matched
String source = "<span title = '5-star hotels 'class = 'dx dx5'> ";
// Convert the string to lowercase
// Source = source. toLowerCase ();
// Regular expression of the matched string
String reg_charset = "<span [^>] *? Title = '([0-9] * [\ s | \ S] * [u4E00-u9FA5] *)' [\ s | \ S]

* Class = '[a-z] * [\ s | \ S] * [a-z] * [0-9] *' ";
 
Pattern p = Pattern. compile (reg_charset );
Matcher m = p. matcher (source );
While (m. find ()){
System. out. println (m. group (1 ));
        }
 }

Java regular expressions can match Chinese characters, and Chinese characters can also be used to write expressions.

The code is as follows: Copy code

String reg_charset = "<span [^>] *? Title = '([0-9] * [\ s | \ S] * Star Hotels) '[\ s | \ S] * class =' [a-z] * [\ s | \ S]

* [A-z] * [0-9] * '";

String reg_charset = "<span [^>] *? Title = '([0-9] * [\ s | \ S] * Star hotels)' [\ s | \ S] * class = '[a-z] *

[\ S | \ S] * [a-z] * [0-9] * '";

Some common regular expression matching rules

Regular Expression Matching Chinese characters: [u4e00-u9fa5]
Note: matching Chinese characters is really a headache. It is easy to use this expression.
Match double byte characters (including Chinese characters): [^ x00-xff]
Comment: It can be used to calculate the length of a string (a dual-byte length meter 2, ASCII character meter 1)
Regular Expression Matching blank rows: ns * r
Comment: It can be used to delete blank rows.
Regular expressions matching HTML tags: <(S *?) [^>] *> .*? | <.*? />
Comments: The versions circulating on the Internet are too bad. The above can only match some of them, and there is still nothing to do with complicated nested tags.
Regular expression matching the first and last blank characters: ^ s * | s * $
Comments: It can be used to delete spaces (including spaces, tabs, page breaks, and so on) at the beginning and end of a line. It is a very useful expression.
Regular Expression Matching the Email address: w + ([-+.] w +) * @ w + ([-.] w + )*. w + ([-.] w + )*
Comment: form verification is very useful
Regular Expression Matching URL: [a-zA-z] +: // [^ s] *
Comments: versions circulating on the Internet have limited functions. The above can basically meet the requirements.
Match account validity (starting with a letter, may be 5-16 bytes, may be an alphanumeric underline): ^ [a-zA-Z] [a-zA-Z0-9 _] {} $
Comment: form verification is very useful
Match Chinese phone number: d {3}-d {8} | d {4}-d {7}
Commentary: matching forms such as 0511-4405222 or 021-87888822
Match Tencent QQ number: [1-9] [0-9] {4 ,}
Comment: Tencent QQ number starts from 10000
Match China Zip code: [1-9] d {5 }(?! D)
Note: China post code is a six-digit number.
Matched ID card: d {15} | d {18}
Note: the ID card of China is 15 or 18 characters
Matched IP address: d +. d +
Comments: useful when extracting IP addresses

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.