Basic Regular expressions can be used to retrieve, select, edit, and validate strings that conform to certain characteristics. It's very versatile, many programming languages provide support for regular expressions at the language level or in class libraries, and many text retrieval tools support regular expressions, and we can also see it when we filter files using git .... A simple example of a test program is to determine whether a string is all composed of numbers. Traditional way of judging:
public class regexdemo01{public static void main (string args[]) { string str = "1234567890";//This string consists of numbers Boo Lean flag = true; Define a tag variable //To first break the string into a character array, then determine char c[] = Str.tochararray ();//Change the string to a character array for (int i=0;i<c.length; i++) {//loop to determine if (c[i]< ' 0 ' | | C[i]> ' 9 ') {//If the condition is met, it is not a number flag = false;//Mark break;//The program will not continue down } } if (flag) { System.out.println ("is made up of numbers! ") ; } else{ System.out.println ("Not made up of numbers! ") ; } } };
Use regular Expressions:
Import Java.util.regex.Pattern;p ublic class regexdemo02{public static void main (string args[]) { string str = "1 234567890 "; This string consists of numbers if (Pattern.compile ("[0-9]+]"). Matcher (str). Matches ()) {//Use regular System.out.println ("is made up of numbers!) ") ; } else{ System.out.println ("Not made up of numbers! ") ; } }};
basic syntax for regular expressions
Table
B |
Specify character B |
\xhh |
Characters with a hexadecimal value of oxhh |
\uhhh |
Hexadecimal representation of Unicode characters as Oxhhh |
\ t |
TAB tab |
\ n |
Line break |
\ r |
Enter |
\f |
Page change |
\e |
Escaping (Escape) |
character class
[ABC] |
Any character that contains a, B, and C (same as A|b|c) |
[^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]] |
Any A, B, C, H, I, J characters |
\s |
Whitespace (Space, tab, line feed, 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 [^\w] |
. |
Match any character except a newline |
Reminder: In Java Regular expressions, except for things like line breaks and tabs, you just need to use the SLR slash: \n\t, other in the encounter \, you need to escape it, such as the use of \d must use \\d, when encountering some other ambiguity, also need to escape, For example, inserting a normal backslash, you should use \\\\, insert a | , you need to use \\|.
How do you mean any character? Since \s represents a whitespace character, and \s represents a non-whitespace, [\s\s] can represent an arbitrary character, and [\s\s]* can represent any string. Similarly, [\d\d], [\w\w], [. \ n] can play the same role.
logical operators
Xy |
Y followed by X. |
X| Y |
X or Y |
X |
Capturing groups |
Match boundary 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 |
quantifier
X? |
One or 0 x |
x* |
0 or one or more X |
x+ |
One or more X |
X{n} |
Exactly n times x |
X{n,} |
At least n times x |
X{N,M} |
X at least n times, and no more than m times |
Pattern, Matcher classThese two classes are the core classes of the regular operation and are defined in the Java.util.regex package. Pattern is mainly to write regular specification, Matcher class is mainly used to execute the specification, verify whether a string conforms to its specification. Common methods of Pattern:
Method |
Describe |
public static Pattern Complie (String regex) |
Returns the regular expression rule |
Public Matcher Matcher (charsequence input) |
Get Matcher Instance |
Public string[] Split (charsequence input) |
String splitting |
Common methods of the MAtcher class:
Method |
Describe |
public Boolean matches () |
Perform validation |
public string ReplaceAll (string replacement) |
String substitution |
Use Example:
public class regexdemo03{public static void main (string args[]) { string str = "1983-07-27";//Specify a string in good one date format
string Pat = "\\d{4}-\\d{2}-\\d{2}"; Specify a good regular expression pattern P = pattern.compile (PAT);//Instantiate the pattern class Matcher m = p.matcher (str);//Instantiate Matcher class if (M . Matches ()) {//To verify the match, use regular System.out.println ("date format is legal! ") ; } else{ System.out.println ("date format is not legal! ") ; } }};
String class support for regularThe regular support provided in string is generally used in daily use, and the pattern and Matcher classes are seldom used. Regular-support methods commonly used in string:
Method |
Describe |
Public boolean matches (String regex) |
String match |
public string ReplaceAll (string regex,string replacement) |
String substitution |
Public string[] Split (String regex) |
String splitting |
Cases:
public class regexdemo06{public static void main (string args[]) { string str1 = "a1b22c333d4444e55555f". ReplaceAll ("\\d+", "_"); Boolean temp = "1983-07-27". Matches ("\\d{4}-\\d{2}-\\d{2}"); String s[] = "a1b22c333d4444e55555f". Split ("\\d+"); System.out.println ("String substitution operation:" + str1); System.out.println ("String validation:" + temp); System.out.print ("Split of String:"); for (int x=0;x<s.length;x++) { System.out.print (s[x] + "\ t");}} ;
Basic use of regular expressions