Dark Horse Programmer--java Learning 16 (BI 25)--Regular expression

Source: Internet
Author: User

Regular expressions

An expression that conforms to a certain rule

Role: Dedicated to manipulating strings

Features: Used for some specific symbols to represent some code manipulation, thus simplifying writing

Benefit: simplifies complex manipulation of strings

Specific operation function:

1. Match:

Public boolean matches (String regex)

Parameters

Regex-a regular expression used to match this string

2. Cutting

String[] Split (String regex)

Splits this string according to the match of the given regular expression.

3. Replace

String ReplaceAll (string regex, string replacement)

Replaces all substrings in this string that match the given regular expression with the given replacement.

4. Get

  

1  Packagelearn;2 3 /*4 * Get5 */6 Importjava.util.regex.*;7 8 classRegexDemo29 {Ten      Public Static voidMain (string[] args) One     { A get (); -     } -      Public Static voidget () the     { -String str = "Ming Tian Jiu Yao Fang Jia le, Dajia."; - System.out.println (str); -String regex = "\\b[a-z]{3}\\b"; +  -         //encapsulates a rule as an object.  +Pattern p =pattern.compile (regex); A  at         //associates the regular object with the string to be played. Gets the match object.  -Matcher m =P.matcher (str); -  -         //System.out.println (M.matches ());//in fact, the matches method in the string class. The pattern and Matcher objects are used to accomplish this.  -                                         //It is simple to use when the string method is encapsulated. But the functions are single.  -          in         //Boolean B = M.find (); - //System.out.println (b); to //System.out.println (M.group ()); +          while(M.find ()) -         { the             //Use the group method to get the result after the match.  * System.out.println (M.group ()); $             //the return values of the start () and End () methods represent the position of the coordinates of the matched result on the string, respectively. Panax NotoginsengSystem.out.println (M.start () + "," +m.end ()); -         } the     } +}

character x character x \ \ backslash character \0n with octal value0 characters N (0 <= N <= 7) \0nn with octal values0 characters nn (0 <= n <= 7) \0mnn with octal values0 characters mnn (0 <= m <= 3, 0 <= N <= 7\xhh characters with a hexadecimal value of 0x hh \uhhhh characters with a hexadecimal value of 0x hhhh \ t tab (' \u0009 ') \ nthe new line (newline) character (' \u000a ') \ r return character (' \u000d ') \f page Break (' \u000c ') \a Alarm (Bell) character (' \u0007 ') \e Escape character (' \u001b ') \cx the control character class corresponding to x [ABC] A, B, or C (Simple Class) [^ABC] Any character except A, B, or C (negation) [a-za-Z] A to Z or A to Z, the letters at both ends are included (range) [a-D[M-P]] A to D or M to p:[a-dm-p] (and set) [a-z&&[def]] D, E or F (intersection) [a-Z&AMP;&AMP;[^BC]] A to Z, except for B and c:[ad-z] (minus) [a-Z&AMP;&AMP;[^M-P]] A to Z, not m to p:[a-lq-z] (minus) the predefined character class. Any character (which may or may not match the line terminator) \d number: [0-9] \d Non-numeric: [^0-9] \s whitespace characters: [\t\n\x0b\f\r] \s non-whitespace characters: [^\s] \w word character: [a-za-z_0-9] \w non-word characters: [^\w] Boundary matching device^The beginning of the line ends at the end of the \b Word boundary \b The non-word boundary \a The beginning of the input \g the end of the last matching \z input, only for the final terminator (if any) \z the end of the input greedy quantity word X?x, no X, once or once*x, 0 times or more x+x, one or more x{n} x, exactly n times x{n,} x, at least n times x{n,m} x, at least n times, but no more than M-times and capture-capturing groups can be numbered by calculating their opening brackets from left to right. For example, in an expression ((A) (B (C))), there are four such groups:1( (A) (B (C)))2\a3(B (C))4(C) group 0 always represents an entire expression. 

Dark Horse Programmer--java Learning 16 (BI 25)--Regular expression

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.