Regular expressions in Java--to be continued

Source: Internet
Author: User

1, about "\", in Java in the regular expression of the difference;

In other languages, "\ \" is expressed as: I want to insert a normal backslash in the regular expression;

In Java, "\ \" is represented as: I want to insert a regular expression back slash;

Eg: the regular expression for validating integers is \\d; If you want to insert a backslash: \\\\;

The newline and tab characters are \ n and \ t;

2. Special characters

Greedy number of words
X ? X, not once or once
X * X, 0 or more times
X + X, one or more times
X {n} X, exactly n times
X {n,} X, at least n times
X {n,m} X, at least n times, but not more than m Times
Reluctant number of words
X ?? X, not once or once
X *? X, 0 or more times
X +? X, one or more times
X {n}? X, exactly n times
X {n,}? X, at least n times
X {n,m}? X, at least n times, but not more than m Times
Possessive number of words
X ?+ X, not once or once
X *+ X, 0 or more times
X ++ X, one or more times
X {n}+ X, exactly n times
X {n,}+ X, at least n times
X {n,m}+ X, at least n times, but not more than m Times

Demo Code:

 Packagecom.st.day20150525; Public classStringTest03 { Public StaticString message = "The class string includes methods for examining" + "individual characters of the sequence, fo R comparing strings, "+" for searching strings, for extracting substrings, and for "+" creating a Copy of a string with the characters translated to ' + ' uppercase or to lowercase. Case mapping was based on the Unicode "+" standard version specified by the Character class. ";  Public Static voidMain (string[] args) {//1, using the built-in function in string, the character of the seriousSystem.out.println ("1243". Matches ("-?\\d+"))); System.out.println ("+911". Matches ("-?\\d+"))); System.out.println ("+911". Matches ("(-|\\+)? \\d+")); //2. Use String.Split () to separate strings from regular expression matchesstring[] Str01 = Message.split ("Of|to");//separate from where or to         for(inti = 0; i < str01.length; i++) {System.out.println ("section" +i+ ":" +Str01[i]); } string[] Str02= Message.split ("\\w");//separate from the place where the spaces are         for(inti = 0; i < str02.length; i++) {System.out.print ("+i+": "+str02[i]+" "); }    }}

Regular expressions in Java--to be continued

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.