Java Basics-Day 26th

Source: Internet
Author: User

Regular expression's regular character:

A: Character

x character X, any character represents itself.

\ \ backslash Character

\ r Enter

\ nthe line break

B: Character class

[ABC] A, B, or C, any character at a time.

[^ABC] Any character except A, B, or C

[A-za-z] A to Z or A to Z, the letters at both ends are included

[0-9] Arbitrary numeric characters at once

C: Predefined character classes

. Any of the characters

\d number: [0-9]

\w Word character: [a-za-z_0-9]

Word characters: English, numerals, and _

D: Boundary Matching device

^ The beginning of the line

End of the $ line

\b Word boundaries (that is, it cannot be a word character)

ABC Hello World?haha

E: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 no more than m times

F: Group

Capturing groups can be numbered by calculating their opening brackets from left to right. Group 0 always represents an entire expression.

((A) (B (C)))

First group: (A) (B (C))

Group II: A

Group III: B (C)

Group Fourth: C

1 ImportJava.util.regex.Matcher;2 ImportJava.util.regex.Pattern;3 4 //application of Pattern class5  Public classPatterndemo {6      Public Static voidMain (string[] args) {7Pattern p = pattern.compile ("A*b");//Regular expressions are represented in parentheses8Matcher m = P.matcher ("C*b");//the string to match in parentheses9         BooleanFlag =m.matches ();Ten System.out.println (flag); OneSystem.out.println ("-------------------------------------------"); A          -String regex = "(.) \\1+ ";//notation of overlapping words, regular expressions -Pattern P1 =pattern.compile (regex); theString str = "JSKKJJVJKSVKJSJKGAHGGGGHGAAAYF"; -Matcher M1 =P1.matcher (str); -         BooleanFlag2 = M1.matches ();//finding out overlapping words - System.out.println (FLAG2); +          while((M1.find ())) {//whether to find, to judge, to automatically move the pointer to the next index -String strtemp = M1.group ();//get to find out the results + System.out.println (strtemp); A         } atSystem.out.println ("-------------------------------------------"); -string[] Strarr = Str.split (regex);//segmentation based on overlapping words -          for(String strtemp:strarr) { - System.out.println (strtemp); -         } -System.out.println ("-------------------------------------------"); in     } -}

Java Basics-Day 26th

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.