Regular expressions
A regular expression (regex) is a string that is used to describe and match a series of strings that conform to a certain syntactic rule
Purpose: Match, cut, replace, get string
Regular expressions are made up of some ordinary characters and some meta-characters.
Common meta characters
^ matches the starting position of the input string
$ matches the end position of the input string
\d matches a numeric character. equivalent to [0-9]
\d matches a non-numeric character. equivalent to [^0-9]
\s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\n\r\t\f]
\s matches any non-whitespace character. equivalent to [^\n\r\t\f]
\w matches any single character that includes an underscore. Equivalent to "[a-za-z0-9
]"
\w matches any non-single character. Equivalent to "[^a-za-z0-9]”
. Match any single character except "\ r \ n"
{n} n is a non-negative integer. Match determined N times
{N,} n is a non-negative integer. Match at least N times
{n,m} m and n are non-negative integers, where n<=m. Matches at least n times and matches up to M
- Matches the preceding subexpression 0 or more times (greater than or equal to 0 times)
- ? Match the preceding subexpression 0 or one time
- Match previous subexpression one or more times (greater than or equal to 1 times)
Understanding of "\"
There are three meanings of backslash "\" in Java:
- A backslash can be followed by a specific character to form the so-called "escape character". eg: \ n \ t
- Used to cancel the meaning of metacharacters, making metacharacters into ordinary characters. eg: "\" stands for "\".
- Used to make up the metacharacters in a regular expression.
Eg: "\d" represents "match a numeric character" in a regular expression.
Pattern class and Matcher class
- Both the pattern class and the Matcher class are defined in the Java.util.regex package.
- The object of the pattern class represents the object after the regular expression is compiled; the Matcher class is primarily used to perform validation.
- The main methods of the pattern class are:
public static Pattern compile (String regex);
Public? Matcher?matcher (charsequence input)
The main methods of the Matcher class are:
public boolean matches ();
String class support for regular expressions
- The public boolean matches (string regex) determines whether a string matches a given regular expression.
- public string ReplaceAll (string regex,string replacement) string substitution
- Public string[] Split (string regex) string split
Java Regular Expressions