Java Regular Expression Learning record

Source: Internet
Author: User
Tags character classes

Java Regular Expression Learning record

 A regular expression (regular expressions) is a method that describes a set of strings, based on the common characteristics of each string in a string set. Regular expressions can be used to search, edit, or manipulate text and data. It goes beyond the standard syntax of the Java programming language, so it is necessary to learn specific syntax to build regular expressions. The changes in regular expressions are complex, and once you understand how they are structured, you can parse or construct arbitrary regular expressions.

Character class

Character class

[ABC]

A, B or C (simple Class)

[^ABC]

Any character except A, B, or C (inverse)

[A-za-z]

A to Z, or A to Z, including (range)

[A-d[m-p]]

A to D, or M to P:[a-dm-p] (set)

[A-z&&[def]]

D,e or F (intersection)

[A-Z&&[^BC]]

A to Z characters other than B and C: [Ad-z] (difference set)

[A-z&&[^m-p]]

A to Z, and does not include M to P:[a-lq-z] (difference set)

Predefined character classes

Predefined character classes

.

Any character (matches or does not match the line terminator)

\d

numeric characters: [0-9]

\d

Non-numeric characters: [^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]

Word Quantity

Type of quantifier

Significance

Greed

Barely

Occupation

X?

X??

x?+

Match X 0 or one time

x*

X*?

x*+

Match X 0 or more times

x+

X+?

X + +

Match X one or more times

X{n}

X{n}?

x{n}+

Match X N Times

X{n,}

X{n,}?

x{n,}+

Match X at least n times

X{N,M}

X{n,m}?

x{n,m}+

Match X at least n times, but not more than m times

Boundary Matching Device

^

Header

$

End of Line

\b

Word boundaries

\b

Non-word boundary

\a

Start of input

\g

End of last match

\z

The end of the input, only for the last terminator (if any)

\z

End of input

Example: Using grouping to go to file extensions

Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;

Public class Foo {

public static string Testfoo (stringsource) {

// two groups

pattern p = pattern. Compile("(\\w+)." (. +)");

Matcher mat = p. Matcher (source);

Mat. Find ();

// print number of groups

System. out . println (Mat. GroupCount ());

// Returns the second grouped content

returnMatGroup (2);

   }

public staticvoid main (string[]args) {

System. out. println (testfoo("User3.java"));

   }

}

Printing results:

2

Java

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Regular Expression Learning record

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.