Application of Regular Expression in Java (2)

zhaozj2021-02-16  66

Now JDK1.4 has finally have their own regular expressions API package. Java programmers can be left to find a third-party regular expression library, and we now come and understand the later Sun offers. - - It is true for me. 1. Introduction: java.util.Regex is a class library package that matches the string with a scheduled pattern. It consists of two classes: pattern and matcher pattern a Pattern is a regular expression after compiled performance mode. Matcher A Matcher object is a status machine that makes a matching check for strings based on the Pattern object as a matching mode. First, a PATTERN instance has formulated a similar regular expression of the syntax used to Perl, and then a Matcher instance performs a string matches under the mode control of this given Pattern instance. Let's take a look at these two classes: 2.Pattern class: Pattern's method is as follows: Static Pattern Compile (String Regex) compiles a given regular expression and gives PatterN Static Pattern Compile (String Regex, int Flags), but increase the designation of the Flag parameter, the optional FLAG parameters include: Case INSENSITIVE, MULTILINE, DOTALL, UNICODE CASE, CANON EQ INT FLAGS () Returns the matching of the current Pattern matching Flag parameter. Matcher matcher (charsequence input) Given the named Matcher object Static Boolean Matches (String Regex, Charsequence Input) Compiles a given regular expression and match the input string with the regular expression, which is suitable for the regular expression. Once, it is only one match job, because in this case, there is no need to generate a Matcher instance. String Pattern () Returns the regular expression compiled by the Patter object. String [] split (charsequence intrut) divides the target string in accordance with the regular expression contained in the Pattern. String [] split (Charsequence Input, INT LIMIT) The effect is the same, the increased parameter LIMIT is the number of segments to specify the number of segments, such as setting the LIMI to 2, then the target string will be divided into two segments according to the regular expression. A regular expression, that is, a string has a specific character, you must first compile as a Pattern class instance, this pattern object will use the Matcher () method to generate a Matcher instance, then you can use the Matcher instance The compiled regular expression is based on the work of the target string, and multiple Matcher can share a Pattern object.

Now let's take a simple example, then analyze it to learn how to generate a Pattern object and compile a regular expression, and finally divide the target string according to this regular expression: import java.util.Regex. *; Public class replacement {public static void main (string [] args) throws exception {// generates a Pattern while compiling a regular expression pattern p = pattern.compile ("[/] "); // with Pattern Split () Method Press the string "/" Split String [] Result = p.split ("Kevin Has Seen" Seveal Times, Because IT Is A Good Film. " " / Kevin has seen "this killer is not Too cold "a few times, because it is a" "good movie ./ Noun: Kevin."); For (int i = 0; i

3. Matcher class: Matcher method is as follows: Matcher appendreplacement (StringBuffer SB, String Replacement) Replacing the current matching substring with a specified string and a string after the substantial substring and its previous matching substrings Add to a StringBuffer object. StringBuffer appendtail (StringBuffer SB) adds the remaining string after the last matching work to a StringBuffer object. INT end () returns the index position of the last character of the currently matched substrings in the original target string. INT end returns the position of the last character of the substrings that match the group specified in the match mode. Boolean Find () tries to find the next matching sub-string in the target string. Boolean Find (int Start) re-sets the Matcher object and attempts to find the next matching substring from the specified location in the target string. String group () Returns all substrs for the current lookup and all substrs for groups. Spring Group, returned to the current lookup substring content int groupCount () returns the matchcount acquisition of the current lookup. The number of groups. Boolean Lookingat () Detects whether the target string starts with a matching substring. Boolean Matches () Try to display the entire target character extension, which is only the true value is returned when the entire target string is fully matched. Pattern Pattern () Returns the existing matching mode of the Matcher object, that is, the corresponding Pattern object. String ReplaceAll (String Replacement) replaces all of the target strings with both a single-mode match to the specified string. String Replacefirst (String Replacement) Replaces the first string of the target string with both a substring of existing patterns to the specified string. Matcher RESET () Reset the Matcher object. Matcher RESET (CharseQuence Input) reset the Matcher object and specifies a new target string. INT Start () Returns the position of the current lookup of the subsequent start character in the original destination string. INT Start Returns the position of the first character in the original destination string in the current lookup obtained and specified groups. (Interpretation of light reading method is not very bad? Don't worry, it will be easier to explain the example) A Matcher instance is used to perform existing patterns based on the target string (that is, a given Pattern The compiled regular expression) Match the lookup, all the inputs to Matcher are provided through the CharSequence interface, which can support the data provided from the diversified data source. Let's take a look at the use of each method: ★ Matches () / Lookingat () / Find (): A Matcher object is generated by a Pattern object to call its matcher () method, once the Matcher object is generated, it can Three different matching findings: matches () methods try to match the entire target character exhibiting detection, which is only the true value is returned when the entire target string is fully matched. The Lookingat () method will detect if the target string starts with a matched substring. The Find () method is trying to find the next matching subster in the target string. The above three methods will return a boolean value to indicate that success or not.

★ ReplaceAll () / appendreplacement () / appendtail (): Matcher class also provides four methods for replacing the matching substrings to specify strings: ReplaceAll () Replacefirst () appendreplacement () appendtail () ReplaceAll () with ReplaceFirst ( The usage is relatively simple, please see the explanation of the above method. We mainly focus on the appendreplacement () and appendtail () methods. AppendReplacement (StringBuffer SB, String Replacement) Replaces the current matching substring to a specified string, and adds the replaced substring and the string segment after the previous matching substring is previously added to a StringBuffer object, and appendtail. StringBuffer SB) Method Adds the remaining string after the last matching work to a StringBuffer object. For example, there is a string FatcatFatfat, assuming existing regular expression mode is "CAT", first matching appendreplacement (SB, "DOG"), then the content of StringBuffer SB is fatdog, which is Fatcat. Cat Replaced with DOG and adds the content before the matching substrings to the SB, and the AppendReplacement (SB, "DOG") is called after the second match, then the content of the SB changes to fatdogfatdog, if finally calls AppendTail (SB ), Then the final content of the SB will be FatDogfatdogfat.

Still a little blurry? Then let's take a simple program: // This example will change "kelvin" in the sentence to "kevin" import java.util.regex. *; Public class matchertest {public static void main (String [] args) throws Exception {// Generate a Pattern object and compile a simple regular expression "Kelvin" Pattern P = Pattern.Compile ("kevin"); // Generate a Matcher object Matcher M = P.matcher ("Kelvin Li and Kelvin Chan Are Both Working In Kelvin Chen's KelvinsoftShop Company"); StringBuffer SB = New StringBuffer (); INT i = 0; // Using the Find () method Find the first matching object Boolean Result = m. Find (); // Use a loop to identify all Kelvin in the sentence and replace the content to add the content to the while (result) {i ; M.AppendReplacement (SB, "Kevin"); system.out.println (" The contents of SB after the " I " secondary match is: " SB); // Continue to find the next matching object result = m.find ();} // finally call the appendtail () method will last after the last match The string is added to the SB; M.AppendTail (SB); system.out.println ("call M.Appendtail (SB) after the final content of SB is:" sb.tostring ());}} The final output result is : The content of the SB after the first match is: Kevin The contents of SB after the second match is: Kevin Li and Kevin 3 times Match SB content is: Kevin Li and Kevin Chan Are Both Working in Kevin 4th match The content of the post-SB is: Kevin Li and Kevin Chan Are Both Working in Kevin Chen's Kevin Call M.AppendTail (SB) After the final content of SB is: Kevin Li and Kevin Chan Are Both Wo RKING IN Kevin Chen's KevinsoftShop Company. Watch the use of this routine for appendreplacement (), and appendtail () two methods are clearer, if it is still not necessary to write a few lines of code test.

★ Group () / group (int group) / groupcount (): This series of methods is similar to the matchresult .group () method in Jakarta-ORO in the above introduction (please refer to the contents of JAKARTA-ORO), It is necessary to return to the substrs that match the group, the following code will be well explained: import java.util.Regex. *; Public class grouptest {public static void main (string [] args) throws exception {pattern P = Pattern.Compile ("(CA)"); matcher m = p.matcher ("One cat, two cats in the yard"); stringbuffer sb = new stringbuffer (); boolean result = m.Find (); System.out.println ("The quantity of the lookup matching group is:" m.GroupCount ()); for (int i = 1; i <= m}}) output is: This time lookup gains the number of matching groups In order to: 2 of the substring content of the first group: the substrs of the CA Group 2 are: other methods of the TMATCher object are better understood and because of the limited space, readers should be progracted. 4. A small test of the email address Program: Finally, let's take a look at the Email address, which is used to verify whether the characters contained in an input Email address are legal. Although this is not a complete Email address inspection program, it cannot be verified that there may be The situation, but you can add the required functions on the basis if necessary.

Import java.util.regex. *; public class email {public static void main (string [] args) throws exception {string input = args [0]; // Detects whether the input EMAIL address is illegally symbol. "or" @ "As the start character pattern p = pattern.compile (" ^ /. | ^ / @ "); Matcher m = p.matcher (input); if (m // detection is" Www. "Is starting P = Pattern.Compile ("^ www /."); M = p.matcher (input); if (m // detection contains illegal characters P = pattern.Compile ("[^ A-ZA-Z0-9 /. / @ _ / - ~ #] "); m = p.matcher (input); stringbuffer sb = new stringbuffer (); boolean result = m.find (); boolean deletedillegalchars = false; while (result) {//// If you find an illegal character, then set the mark deletedillegalchars = true; // If the illegal character is included, if the illegal character is double quotes, then eliminate them, add M.AppendReplacement (SB, ") (SB,"); result = m .find ();} m.appendtail (sb); input = sb.tostring (); if (deletedillegalchars) {system.out.println ("Input email address contains illegal characters such as colon, comma, please modify" System.out.Println ("Your current input is:" args [0]); system.out.println ("The legal address after modification is similar:" input);}}}, for example, we are Command line input: Java email www.kevin@163.net The output will be: Email address cannot start with 'www.' Start If the input email is @ kevin @ 163.net output is: Email address cannot be . 'Or' @ 'as the start character when the input is: cgjmail # $% @ 163.net The output is: Input email addresses include colon, comma, etc. Method character, please modify your current input as: cgjmail# $ 00@163.net The legal address should be similar: cgjmail@163.net 5. Summary: This article describes the classes in the JDK1.4.0-beta3 in the regular expression library - Java.util.Regex, and its method, if combined with the Jakarta-Oro API introduced in the previous, readers will be easier Mastering the use of the API, of course, the performance of the library will continue to expand in the future, I hope to get the latest information readers to understand the website to Sun's website. 6. Conclusion: Originally, I will write more about the regular expression library that needs to pay, but I feel that since I have a free and excellent regular expression library can be used, why bother to find a payment , I believe many readers are also thinking about:, so I am interested in learning more other third-party regular expressions, friends who can find themselves to find them online or to see the URLs provided in the reference.

转载请注明原文地址:https://www.9cbs.com/read-16021.html

New Post(0)