Regular Expression Learning (Java)

Source: Internet
Author: User

It took another night to review the video of the Java regular expression. Then I wrote a small program to test it. Some special syntaxes are not tested. I have a rough idea about them. You need to try again later. It is not difficult to understand regular expressions, but it is difficult to use them skillfully and practically.

Public class test1 {<br/>/* <br/> * test the regular expression syntax <br/> */<br/> Public static void main (string [] ARGs) {<br/> test1 T = new test1 (); <br/> T. testxiegang (); // test the slash/<br/> T. testreplaceall (); // test the string class replace method <br/> T. testzhongkuohao (); // test [] <br/> T. testpredefchar (); // test/S/W/D/S/D/W in uppercase. <br/> T. testboundmatch (); // test boundary match <br/> T. testlogic (); // test the logical operator <br/>}< br/> private void testxi.pdf Ng () {<br/>/* <br/> * the string represents a continuous character sequence. in the program, we use the string class to describe a string, <br/> * the string described in string is not necessarily the same as the actual string. For example, how to express double quotation marks in a string? How to express some blank characters, so we must introduce the concept of escape characters in the <br/> * string class, that is /. For example, string STR = "AB/T //" B "actually represents AB/" B, which is a sequence of 7 characters. <Br/> * a regular expression is a special expression, but it must comply with these rules. For example, string STR = "//" is actually two common characters // and then compiled with a regular expression, <br/> * only one/<br/> */<br/> system. out. println ("//". matches ("//"); // true two/must be expressed in the regular expression // <br/> system. out. println ("// n ". matches ("// n"); // true <br/> string STR = "Ni/T // hao/" an "; <br/> system. out. println (STR); // output Ni/Hao "An. This is the actual string <br/> system. out. println (Str. matches (". {10} "); // true STR is composed of 10 characters <br/>}< br/> private void testre Placeall () {<br/> string STR = "Niha + ********* Oma? /T "; <br/> STR = Str. replaceall (" [?] "," "); // Separate? (+ * Can be used separately.) The problem may occur because [] is used for inclusion or //? You can <br/> system. out. println (STR); // Niha + ********* Oma <br/> STR = Str. replace ("+", ""); <br/> system. out. println (STR); // Niha ********* Oma <br/> STR = Str. replace ("*", ""); <br/> system. out. println (STR); // nihaoma <br/> system. out. println ("92 ". matches ("(// D +)"); // true parentheses are also special characters <br/> system. out. println ("(92 )". matches ("// (// D + [)]"); // true parentheses are also special characters, use [] or <br/>}< br/> private void testzhongkuohao () in the Regular Expression () {<br/> string STR = "Da Jia Hao Cai Shi Zhen de Hao"; <br/> string str1 = "abdbca"; <br/> system. out. println ("A ^ C ". matches ("[A-Z ^ A-Z] {3}"); // true ^ only the first position in [] indicates no, otherwise it is a common character <br/> system. out. println ("ABC ". matches ("[^ A-Za-Z] {3}"); // false ^ indicates no, it is false; <br/> system. out. println ("ABC ". matches ("[A-N & G-Z] {3}") + "" + "Hin ". matches ("[A-N & G-Z] {3}"); // false true <br/> system. out. println ("EFG ". matches ("[^ A-D & B-G] +"); // true a, B, c, D is false e f g is true <br/> system. out. println ("BCD ". matches ("[A-D & ^ B-G] +"); // true ^ is a common character in the middle <br/> system. out. println ("AAA ". matches ("[A-D & [^ B-G] + ")); // true only a matches <br/>}< br/> private void testpredefchar () {// test the pre-character class <br/> system. out. println ("A/n/Ta ". replaceall ("// s", ""); // AAA is equivalent to [/T/N/x0b/F/R] <br/> system. out. println ("(& ^ * liujiyong814329735 ". replaceall ("// W", ""); // (& ^ * equivalent to [a-zA-Z_0-9] <br/> system. out. println ("liu8143 ji29735 Yong ". replaceall ("// D", ""); // 814329735 <br/>}< br/> private void testboundmatch () {// test boundary matching <br/> system. out. println ("Java (Java) Java/tjavajava, Java ". replaceall ("// bjava // B", ""); // () javajava,/B indicates the word boundary <br/> system. out. println ("Java (Java) Java/tjavajava, Java ". replaceall ("^ Java", ""); // (Java) javajava, Java ^ indicates the start of a string <br/> system. out. println ("Java (Java) Java/tjavajava, Java ". replaceall ("Java $", ""); // Java (Java) javajava, $ indicates the start of a string <br/>}< br/> private void testlogic () {// test the logical operator | <br/> system. out. println ("256 ". matches ("(// d {1, 2}) | (1 // d {2}) | (2 [0-4] // D) | (25 [0-5]) "); // false <br/> system. out. println ("0 ". matches ("(// d {1, 2}) | (1 // d {2}) | (2 [0-4] // D) | (25 [0-5]) "); // true <br/> system. out. println ("56 ". matches ("(// d {1, 2}) | (1 // d {2}) | (2 [0-4] // D) | (25 [0-5]) "); // true <br/> system. out. println ("156 ". matches ("(// d {1, 2}) | (1 // d {2}) | (2 [0-4] // D) | (25 [0-5]) "); // true <br/> system. out. println ("255 ". matches ("(// d {1, 2}) | (1 // d {2}) | (2 [0-4] // D) | (25 [0-5]) "); // true <br/> system. out. println ("99 ". matches ("// d {1, 2} | 1 // d {2} | 2 [0-4] // d | 25 [0-5]"); // true <br/> system. out. println ("2017192.0.1 ". matches ("(// d {1, 2} | 1 // d {2} | 2 [0-4] // d | 25 [0-5]) //.) {3} (// d {1, 2} | 1 // d {2} | 2 [0-4] // d | 25 [0-5]) "); // true IP Regular Expression </P> <p> system. out. println ("B ". matches ("[A-D & B-G]"); // true B c d matches this regular expression <br/> system. out. println ("A & B ". matches ("A & B"); // true is outside [] & only common characters <br/> // system. out. println ("". matches ("A | BC | C"); // true <br/> // system. out. println ("BCD ". matches ("A | BC | C"); // false <br/> // system. out. println ("C ". matches ("A | BC | C"); // true <br/> // system. out. println ("92 ". matches ("// d {1, 2}"); // true <br/>}< br/>

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.