§1 black dark years
There is a string, how do you query? Is there Y and F characters? The Dark way is:
Program 1: I know if, for statement and charat ().
Class test {
Public static void main (string args []) {
String str = "for My Money, THE IMPORTANT Thing"
"About themeing was bridge-building";
Char x = 'y';
CHAR Y = 'f';
Boolean Result = false;
For (int i = 0; i CHAR Z = Str.Charat (i); //system.out.println (Z); IF (x == z || y == z) { Result = TRUE; Break; } Else Result = false; } System.out.println (Result); } } It seems to be very intuitive, but this way is difficult to deal with complex work. If you find a paragraph, is there an IS? Is there a string or ting. This is an annoying job. §2 java java.util.regex package According to the object-oriented ideas, put the strings that you want to queries such as IS, Thing or Ting into an object, and make a text as a template as a template, it is more natural. The thing as a template is the regular expression to be discussed below. Don't consider so complicated first, look at an example: Program 2: Don't understand. Let's take a look. Import java.util.regex. *; Class Regex1 { Public static void main (string args []) { String str = "for My Money, THE IMPORTANT Thing" "About themeing was bridge-building"; String regex = "a | f"; // means A or F Pattern P = Pattern.Compile (regex); Matcher M = P.matcher (STR); Boolean Result = m.find (); System.out.println (Result); } } If STR matches regex, then RESULT is true, otherwise Flase. If you want to ignore your case when you look up, you can write: Pattern P = Pattern.Compile (regex, pattern.case_insensitive); Although I don't know the details of Pattern (Templates, Mode) and Matcher (Matcher), the feeling of the program is more cool. If IS is queried first, then we must query Thing or Ting, we only need to modify the template pattern, not consider If statements and for statements, or by charat (). 1. Write a special string - regular expression such as A | F. 2, compile regular expressions into a template: P 3, use the template P to match the string STR. The idea is clear, and now how Java is dealt with (Java programmer can use these classes until JDK1.4. §3 Pattern class and lookup 1PUBLIC Final Class Java.util.Regex.pattern is a regular expression compiled expression. The following statement will create a pattern object and assign a value to the handle P: pattern p = pattern.Compile (Regex); interesting, the Pattern class is a Final class, and its constructor is private. Maybe someone tells you something about design patterns, or check the relevant information yourself. The conclusion here is that the Patter Class cannot be inherited, we can't create the object of the Pattern class through NEW. Therefore, in the pattern class, two overloaded static methods are provided, and the return value is the Pattern object (reference). Such as: Public static pattern compile (string regex) { Return New Pattern (Regex, 0); } Of course, we can declare the handle of the Pattern class, such as Pattern P = NULL; 2p.matcher (STR) represents the matching of a string STR to generate a string STR with template P, which is a reference to a Matcher class. Why do you want this thing? According to the natural idea, return a boolean value not? We can simply use the following methods: Boolean Result = pattern.Compile (regex) .matcher (str) .find (); Oh, it is actually the three statement combined without a handle. No handle is often not a good way. Laide the Matcher class later. Let's take a look at the regex - this strange. §4 Regular expression definition Regular Expression is a string that generates strings. Halo. For example, String Regex = "ME "; here the string ME can generate: ME, Mee, Meee, Meeeeeeeeeeeee, etc., a regular expression may generate an infinite string, so we are impossible (necessary ?) Output all things generated by regular expressions. In turn, think about the string: ME, Mee, Meee, Meeeeeeeee, etc. Can we have a language to describe them? Obviously, the regular expression language is this language, which is a single string mode - a simple and profound description. We use regular expressions for string lookups, match, specify string replacement, string segmentation, and the like. Generating strings of strings - regular expressions, it is a bit complex because we want to describe any string by normal characters (such as characters a to z) and special characters (called metammatics), and accurate. First, engage in a few regular expressions: Program 3: We will always test regular expressions. Import java.util.regex. *; Class Regex1 { Public static void main (string args []) { String str = "for my mother, the import"; String regex = "ab *"; Boolean Result = pattern.Compile (regex) .matcher (str) .find (); System.out.println (Result); } } // Ture 1 "AB *" - can match A, AB, ABB, ABBB .... So, * means that the previous characters can be zero or multiple times. If you only consider the lookup, use "A" directly. But think about replacement. Question Regex = "ABB *" results? 2 "AB " - can match AB, ABB, ABBB .... Equivalent to "abb *". How is the problem with the problem regex = "or "? 3 "OR?" - can match O and OR. ? Indicates that the previous characters can be zero or once. These qualifiers *, , which are convenient to represent the number of times the previous character (substring) (we described {}): X * zero or multiple ≡ {0,} X once or multiple ≡ {1,} x? zero or once ≡ {0, 1} x {n} n times (n> 0) x {n, m } At least n times to maximum M times (0 Now we know the lookup of the continuous string, match. The following is some exercises: 1 Find a crude string (not required to accurately or require precise matching), write its regular expression: Strregex (not required) Regex (requires accurate) Try ABCFFDB or BCFF or BCF * or BC * or BC BCFF or BCF {2} BC {3} Goooogleo {1,}, O O {5} Banana ( AN) (AN) {2} a, a (na) {2} 2 Regular expressions match the string, what is the output? §5 Replacement (delete), Matcher class Now we may be tired of True / False, let's take a look at the replacement. For example, replace Book, Google is BAK (this file suffix name is ok in EditPlus), Look or GoOogle. Program 4: Replacement of strings. Import java.util.regex. *; Class Regex1 { Public static void main (string args []) { String regex = "a "; // means one or more A String str = "abbbaaa an banana hhaana"; Pattern P = Pattern.Compile (regex); Matcher M = P.matcher (STR); String s = m.replaceAll ("⊙"); // ("") Delete System.out.println (s); } } The difference between this program and the previous program is to use the M.ReplaceAll (String) method. It seems that the Matcher class is also a bit used. 1 Public Final Class Matcher is a match. You can see him as a person, hold the mold (object of the Pattern class), take a character sequence to match the character sequence by explaining the mold. Match Operations. Often we can program: "Hey, the mold P, you and the string STR create a matching object". Matcher M = P.matcher (STR); 2 m can do some operations, such as Public String ReplaceAll (String Replacement), which replaces all matching strings with Replacement. §6 Special characters for regular expressions We are familiar with such a string "/ n" such as: system.out.print (S "/ nbbb"); this is one of the transfer characters commonly used in Java. In fact, the transfer character is a regular expression, which uses special characters /. The following is a special character commonly used in regular expressions: Matching number symbol * ? {n}, {n,}, {n, m} "or" symbol | program 2 has been used with a sentence symbol. The period symbol matches all characters (one), including spaces, TAB characters, and even wrap. Square bracket [] only matches the characters of the square brackets) Parentheses () packets, the characters in parentheses are treated as a whole. Connect characters - represents a range. "No" symbol ^ indicates the character that does not want to be matched (excluded) We can't learn too much thing at all, this is not all content and usage of regular expressions. But it is enough for us to live. We verify using procedures 4. (⊙⊙ indicates the alternative character) 1 When Regex is the following string, what can I say? Regex matching test STR (A | b) {2} aa, ab, bb, baaabbfooaabfooabfooba [abc] baab, abb, acb3dfacb5ooyfo6abbfocoaab. All string3dfaca. AA, AX ... Wait 3DFACGGD [^ j] Adaa, D9A, etc. In addition to Dja3DFacGdjad5a [DG] [AC] CDAC, ECC, GAC, etc. 3dFacGGGCCAD5C [DG]. {2} cd⊙p⊙c ... 3dffactgggccad5cg {1,10} g, ggg ... 3dfacgggccad5c [A | C] [^ a] 3dFacGGGCCAD5C 2 How do the following strings use Regex? Test STR matches regexaabbfoawaobaofobob a⊙ba..baabbfoaabfooafboba⊙b, in addition to AABA [^ A] B, GooooOogleoooo ... "Tan", "Ten", "Tin" in a book And "Ton" TN, T [AEIO] NABCACCBCBAACABCCAA Remove AC, CA (CA) | ABCCBCBAABCA Remove AB, BA Results CCBCCA (How to Merge with the above) Note: 1. String str = "Tan, Ten, Tin and Ton in a book"; Output: ⊙⊙, ⊙⊙, ⊙⊙ and ⊙⊙ 2, string str = "abcaccbcbaacabccaa"; output: CCBCCA Program 5: IF, for statement, and charat (), 886. Import java.util.regex. *; Class Regex1 { Public static void main (string args []) { String str = "abcaccbcbaacabccaa"; String regex = "(AC) | (CA)"; Pattern P = Pattern.Compile (regex); Matcher M = P.matcher (STR); String s = m.ReplaceAll (""); // ⊙⊙ Regex = "(ab) | (ba)"; p = pattern.Compile (Regex); S = p.matcher (s) .ReplaceAll (""); System.out.print (s "/ n"); } } §7 start It seems that we know some regular expressions and Java knowledge, in fact, we have just started. Here are something we know, and it is also something we don't know. 1 Java introduced (Java.util.Regex package) in JDK1.4 to support the regular expression, two classes in the package, are pattern and matcher, respectively. They all have a lot of ways, we still don't know. SPLIT, MATCHES methods, etc. in the String class also use the regular expression. Is StringTokenizer no use? 2 Regular expressions are a language. There are many regular expressions, options, and special characters, you can view it in the Pattern.java source file. Maybe complicated than imagination. System learning regular expression history, grammar, all special characters (equivalent to keywords in Java), combined logic is the next thing. 3 Regular expressions are important techniques for text processing, widely supported in Perl, PHP, Python, JavaScript, Java, and C #. It is listed as "guarantees ten key technologies that you are not uneasy in the future", huh, huh, let me not believe from you.