Java Regular Expression

Source: Internet
Author: User
Package COM. CG. tests; import Java. util. regEx. matcher; import Java. util. regEx. pattern; import Org. JUnit. test; public class testpatternmatcher {@ testpublic void testreplaceall () {string S = "Hello world... "; string S2 = S. replaceall ("\\. "," "); system. out. println (S2); string S3 = S. replaceall ("\ s", ""); system. out. println (S3); string slinesep = system. getproperty ("line. separator "); string S4 =" abcdefg "+ slinesep + "China"; system. out. println (S4); system. out. println (s4.replace (slinesep, "newline"); string S5 = "1985-01-17, born on this date 23423-2384-"; pattern = pattern. compile ("\ D +"); matcher M = pattern. matcher (S5); While (M. find () {system. out. println (M. group () + "[" + M. start () + "]-[" + M. end () + "]");} system. out. println ("##################"); string S6 = "just do it ~! 123abc @ 1234efg "; // pattern PDO = pattern. compile ("do \ s (do \ s) +"); pattern PDO = pattern. compile ("\ D + (\ W +)"); matcher MDO = PDO. matcher (s6); While (MDO. find () {for (INT I = 0; I <MDO. groupcount (); I ++) {// system. out. println (MDO. group () + "[" + MDO. start () + "]-[" // + MDO. end () + "]"); system. out. println (I + ":" + MDO. group (I) ;}}@ testpublic void testgreedy () {string html = "<Table> <tr> <TD> 123 ksdjfsld </TD> <TD> 34243 kdljsfkldsf </TD> kdlfjsdf </tr> </table "; matcher M = pattern. compile ("<TD> (\ D +) (\ W +) </TD> "). matcher (HTML); // non-Greedy mode while (M. find () {int CNT = m. groupcount (); system. out. println (CNT); For (INT I = 0; I <= CNT; I ++) {system. out. println ("group" + I + ":" + M. group (I) ;}}@ testpublic void testgreedy2 () {string html = "klsdjfl83943875921_ksjd"; matcher M = pattern. compile ("\ D +? "). Matcher (HTML); // non-Greedy mode while (M. Find () {system. Out. println (M. Group ());}}}

  

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.