Java Regular Expression (2)

Source: Internet
Author: User

Regular Expression: A Regular Expression
Role: used for special string operations
Features: it is used to indicate some code operations by symbols of some features. This simplifies writing.
Learning regular expressions is to learn the use of some special symbols.
Specific operation functions:
1. Match: matches () of the string class ()
2. Cut: Split () of the string class ()
3. Replace: replaceall () of the string class ()
4. Get: retrieve the strings that comply with the rules.
(1) encapsulate regular expressions into objects
(2) associate the regular object with the string to be operated.
(3) obtain the regular expression matching engine after Association.
(4) use the engine to operate the strings that comply with the rules, such as extracting them.
{N} specifies the remaining nine digits, which cannot be more than less;
{N, m} can only be n to the next M-bit. the string to be matched can be extra M-bit, but not less.
{N,} indicates all
"(.) \ 1" Completes cutting by overlapping words. In order to reuse the results of rules, rules can be encapsulated into a group.
Complete with (), and the group is numbered. Starting from 1,
To use an existing group, you can use \ n (n is the group number.
"\\\\" Cut \\

"+": "+" Occurs once or multiple times. If you use ".", you must use "\." for escape.

Class regexdemo {public static void main (string [] ARGs) {// demo (); checktel (); // splitdemo ("C: \ ABC \ a.txt ", "\\\\"); // cut by \\// splitdemo ("erktyqquiio ","(.) \ 1 "); // replacealldemo (" we54544456464sy455446dsdsa5451 "," \ D {0,} "," # ");} public static void demo () {string STR = "56566565666666464"; // char [] Buf = Str. tochararray (); string Reg = "[1-9] [[0-9] & [^ A-Za-Z] {4, 14}"; Boolean B = Str. matches (REG); system. out. println (B);} // matches the mobile phone number 13xxx 15xxx 18 xxxpublic static void checktel () {string Tel = "151723174065 "; string telreg = "1 [358] \ D {3,}"; Boolean flag = Tel. matches (telreg); system. out. println (FLAG);} public static void splitdemo (string STR, string REG) {// string STR = "zhangsan Lisi wangwu"; // STR = "C: \ ABC \ a.txt "; // string Reg =" + "; // string [] arr = Str. split (REG); For (string S: ARR) {system. out. println (s) ;}} public static void replacealldemo (string STR, string Reg, string newstr) {STR = Str. replaceall (Reg, newstr); system. out. println (STR );}}

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.