Regular Expression Basics
Regular expression Syntax (1)
Ordinary characters: Letters, numbers, men, underscores, and punctuation that are not specifically defined are "normal characters". An ordinary character in an expression that matches the same character when matching a string
\ n: Represents a line break
\ t: Represents a tab
\ \: represents \ Itself
\^ ...... : The representation matches the character itself
Regular expression Syntax (2)
expressions that can match multiple characters
Attention is case-sensitive, uppercase is the opposite meaning
\d: Any number, any one of the 0~9
\w: Any letter or underscore or kanji, or any of the a~z,a~z,0~9
\s: Any of the whitespace characters, including spaces, tabs, newline characters, and so on.
. : The decimal point can match any one character
Regular expression Syntax (3)
[] square brackets match, can match any one character in square brackets
[[email protected]]: matches any one of the characters in square brackets
[^ABC]: matches any character except ABC
[F-k]: matches any one of the characters between "F"-"k"
[^a-f0-3]: matches any one of the non-a-f and non-0-3 characters
Regular expression Syntax (4)
- Special symbols for modifying the number of matches
{n}: Expression repeats n times
{M,n}: expression repeats at least m times and repeats up to n times
{m,}: expression repeats at least M-times
? : Match expression 0 or 1 times, equivalent to {0,1}
+: expression appears at least 1 times, equivalent to {1,}
*: expression does not appear or occurs any time, equivalent to {0,}
The greedy pattern in the number of matches (the more matches the character, the better, the default)
The non-greedy pattern in the number of matches (the fewer matching characters the better, the special symbol that modifies the number of matches, plus one "? "No.). Example: \d{2,4}?, this changes the mode to non-greedy
Regular expression Syntax (5)
- character boundary (this group of tags matches not a character but a position, where a condition is met)
^: matches the position where the string starts (in one line)
$: matches where the string ends
\b: Match word boundaries//For example boy\b is not a boundary mismatch on boy's right
Using regular expressions and common functions in Java
public class Testregex {public static void main (String []args) {//pattern p = pattern.compile ("^\\d{4}"); The backslash in Java is to be escaped with two preceding, because \ in \d is ambiguous with the Pattern p = pattern.compile ("(\\d{4}) ([a-z]{2})"); Matcher m = P.matcher ("Asdfsadfsafd2342314asdfsadf2342asdfsaf"); System.out.println (M.matches ());//Match entire target string System.out.println (M.find ());//Find string list<string> list = new Arraylist<string> ();//used to store the found Groupwhile (M.find ())//m.find () find one and go for the next {System.out.println (M.group ());// Group (), group (0) has the same function as System.out.println (M.group (0)); System.out.println (M.group (1)); System.out.println (M.group (2));//If write 3 will be out of range List.add (M.group ());} string []STR = "aa33fdf55cc". Split ("\\d{2}");//divide string str2 = "Asd2dsfdfds" by two integers. ReplaceAll ("\\ds", "* *");//Replace DS , the new string is generated because the constants above are immutable System.out.println ("Asdfsdf". Matches ("\\w+"));//Match String}}