What is a regular expression and an application in Java1.4

xiaoxiao2021-04-05  269

Here, PHP is used to cause the following content:

A regular expression is a specific format mode that can be used to find a string in another string. Several programming languages, including Visual Basic, Perl, JavaScript, and PHPs support regular expressions, I hope to end in this entry guidance, Mitchell (author yourself) allows you to apply some basic regular expressions in PHP programs. Regular expressions are a kind of quirky feature in a variety of programs, but because they look hard, many developers put them in the corner, forgot their exist.

Let's take a look at what is a regular expression, why you want to use them in a PHP program.

What is a regular expression?

What do you think about the procedures like bbedit and notepad from a nice old-based text editor? Both support text input, allowing you to save text into the file, but the current text editor also supports other features, including find-instead of tool, which makes the edit a text file is quite easy. Regular expressions are similar, just better. Regular expressions can be considered an extremely advanced lookup - replacement tool, let us get rid of pain from pain: no need to write custom data confirmation examples to check the email address or to confirm the format of the phone number is correct, so wait . One of the most common functions in any program is that the data validity check, PHP bundles some text check functions, allow us to match a string with regular expressions, confirm that there is a space, have a question mark, and so on. You don't know, maybe the regular expression can be easily equipped, when you have some regular expressions (this regular expression can be used to tell the regular expression engine a string we want to match the part), you I will ask why you will throw the regular expression in the corner for so long, ^ _ ^. PHP has two sets of functions to handle two types of regular expressions: Perl5 compatible mode, and POSIX standard compatibility mode. In this article we will look at the EREG function, working with the POSIX standard search expression. Although they do not have the Perl5 model, a good way to learn the regular expression. If you are interested in PHP-supported Perl5 compatible regular expressions, you can find some details about the preg function to the PHP.NET website. PHP has six functions to handle regular expressions, they all take a regular expression as their first parameters, listed below:

EREG: The most commonly used regular expression function, EREG allows us to search for a string matching with a regular expression. EREG_REPLACE: Allows us to search for a string matching with regular expressions, and replacing all this expression with new strings There is a place that appears. EREGI: Almost the same effect as EREG, but ignores case. EREGI_REPLACE: The same search-replacement feature and EREG_REPLACE are ignored. Split: Allows us to search and regular expressions match the string, and return match results in a string collection. Spliti: Split function ignores the case. Version. Why use regular expressions?

If you constantly establish a different function to check or operate a part of the string, now you may want to give up all these functions, replaced by the regular expression. If you answer the following questions, then you must consider using regular expressions:

Are you writing some custom functions to check form data (such as a @, a point in the email address)? Do you write some custom functions, loop each character in a string, if this character matches A specific feature (such as it is uppercase, or it is a space), then replace it? In addition to being uncomfortable string check and operation method, if there is no efficient writing code, the above two will Slow your program slow down. Are you more preferred to check an email address with the following code: In summary: Regular expression makes us in code editing, do not write complex functions to customize input information, such as we need to judge whether you are input in Text For email. The ancient way is to write a custom function to read this string text, and save a lot of code by using the regular expression!

Compare applications in PHP:

... or use the following code:

The first use of the function function for verification, and the following example uses the regular expression library function

Let's take a look at the application of regular expressions in J2EE

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

From the above example, we can compare the Java.util.Regex package in constructing the Pattern object and compiling the specified regular expression. The JAKARTA-ORO package introduced in the previous one is done in the same work. JAKARTA-ORO Package First Constructs a PatternCompiler class object then generates a Pattern object, and then compile the required regular expression with the compile () method of the PatternCompiler class: PatternCompiler OROCOM = New Perl5compiler (); PATTERN PATTERN = OROCOM.Compile ("Regular Expressions"); patternmatcher matcher = new perl5matcher (); but in the java.util.regex package, we only need to generate a Pattern class, use its compile () method You can achieve the same effect: pattern p = pattern.Compile ("[/] "); therefore, it seems that Java.util.Regex is more concise than Jakarta-ORo and is easily understood. 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 contents of the post-SB are: Kevin Li and Kevin Chan Are Both Working In Kevin Chen's Kevin calls M.Appendtail (SB) The final content of SB is: Kevin Look at Kevin Chan's KevinsoftShop Company. Watch the use of this routine for the use of appendreplacement (), appendtail () two methods, if it is still not necessary to write a few lines of code yourself have a 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 ("This time the quantity of the matching group is:" m.GroupCount ()); for (int i = 1; i <= m.GroupCount (); i ) {system.out.println ("The" i "group is:" m.Group (i));}}}} The output is: the number of matches the number of matching groups is: 2 The substrs of the first group are: The substring content of the Group 2 of the CA is: other methods of t Matcher objects are more understanding and because of the limited space, readers should be progracted. 4. A small program for testing an email address: Finally, let's look at a routine that test 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 is a possible 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.find ()) {system.err.println ("Email address cannot be used as a start character");} // detection is "WWW." To start p = pattern.compile ("^ www //."); M = p.matcher (Input); if (M.Find ()) {system.out.println ("Email address cannot start with 'www.' start");} // Detection whether to include illegal characters P = pattern.Compile "[^ A-za-z0-9 /////@_//- ~ #] "); m = p.matcher (input); stringbuffer sb = new stringbuffer (); boolean results = m.find (); Boolean deletedillegalchars = false; while (result) {// If you find illegal characters, then set the mark deletedillegalchars = true; // If the illegal character is included, if the illegal characters are like a colon, then they eliminate them, add them. SB M.AppendReplacement (SB, "); result = m.find ();} m.appendtail (sb); input = sb.tostring (); if (deletedillegalchars) {system.out.println (" Input The email address contains illegal characters such as colon, comma, please modify "); System.out.Println (" Your current input is: " args [0]); system.out.println (" After modifying the legal address Should be similar: " input);}}}, for example, we enter: Java Email Www.kevin@163.net The output result will be: Email address cannot start with 'www.' Start If the input email is @ kevin @ 163.net, the output is: Email address cannot be "@ ' As the start character when the input is: cgjmail # $% @ 163.net, then the output is: Enter the email address containing illegal characters such as colon, comma, please modify your current input: cgjmail# $%@163.net The legal address after modification 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.

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

New Post(0)