Java Regular Expressions

Source: Internet
Author: User

I. Some features of regular expressions in Java

1.java \ \ Represents a regular expression, so ^\d+ (\.\d+), can match the instance: "5", "1.5" and "2.21". But in the program to write ^\\d+ (\\.\\d+)?

2. The normal backslash is expressed as: \\\\

  

Two. String built-in regular expression function

1.boolean matches (string regex) verifies that the string matches the given regex (regular expression)

Cases:

System.out.println ("+911". Matches ("-|\\+)" \\d+))//true

2. string[] Split (string regex) string split

String[] Split (String regex, int limit)

3. Replace

  String Replacefirst (string regex,string replacement)

String ReplaceAll (string regex,string replacement)

Three. Pattern and Matcher

Pattern: Mode Matcher: Match (Engine)

Customary usage:

Pattern pattern == pattern.matcher (text);

After getting the Matcher object, using the method on Matcher, we can determine whether the different types of matches are successful

Boolean matches () Boolean Lookingat () Boolean Find () boolean find (int  start)int  start ()int End ()

Four. Regular expression syntax

Character

B Specify character B
\xhh Characters with a hexadecimal value of oxhh
\uhhhh Hexadecimal representation of Unicode characters as Oxhhhh
\ t TAB tab
\ n Line break
\ r Enter
\f Page change
\e Escaping (Escape)

Character class

. Any character
[ABC] Any character that contains a, B, and C (same as A|b|c action)
[^ABC] Any character except A, B, and C (negation)
[A-za-z] Any character (range) from A to Z or from A to Z
[Abc[hij]] Arbitrary a,b,c,h,i and J characters (same as A|B|C|H|I|J) (consolidated)
[A-z&&[hij]] Any h,i or J (cross)
\s whitespace characters (spaces, tab, wrap, page feed, and carriage return)
\s Non-whitespace character ([^\s])
\d Number [0-9]
\d Non-digital [^0-9]
\w Word character [a-za-z0-9_]
\w Non-word character [a-za-z0-9_]

logical operators

Xy Y followed by X.
X| Y X or Y
X Capturing group (capturing group) can refer to the I-capturing group in an expression using \i

Boundary Match character

^ Start of a row
$ End of Line
\b The boundary of the word
\b The boundaries of non-words
\g The end of the previous match

Quantifiers

Greedy mode Hunger mode Exclusive mode
X? X?? x?+ Match 0 or 1 times
x* X*? x*+ Match 0 or more times
x+ X+? X + + Match 1 or more times
X{n} X{n}? x{n}+ Match n Times
X{n,} X{n,}? x{n,}+ Match at least N times
X{n, M} X{n, M}? X{n, m}+ Matches at least n times, up to M times

Java Regular Expressions

Related Article

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.