To give a simple example, in a need for registration of the B/S mode of application, in the browser to verify the legality of the user registration form is necessary, but you in order to prevent hacker, in the server to verify again must be necessary.
Topic: Verify that the mailbox is legal on the server side: Usually you might write this:
public boolean checkemaillegal (String temp)
{
//temp = "ddenfj# @fe_dw. comw";
Return Temp.matches ("^ ([\\.a-za-z0-9_-]) +@" ([a-za-z0-9_-]) + ((\\.[ a-za-z0-9_-]{2,3}) ({1,2}) $ ");
Of course this will meet your needs, but today I would like to share with you the use of pattern to improve the efficiency of using the experience, first of all, I will take a look at the small facet code of the simple test:
Import Java.util.regex.Pattern;
public class SimpleTest {public
static void Main (string[] args)
{
String temp = "ddenfj# @fe_dw. comw";
int count = 1;
Long a = System.currenttimemillis ();
for (int i=0;i<count;i++)
{
temp.matches ("^" [\\.a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + ((\\.[ a-za-z0-9_-]{2,3}) ({1,2}) $ ")
;
Long B = System.currenttimemillis ();
System.out.println (b-a);
A = System.currenttimemillis ();
Pattern Emailpattern = Pattern.compile ("^ ([\\.a-za-z0-9_-]) +@" ([a-za-z0-9_-]) + ((\\.[ a-za-z0-9_-]{2,3}) ({1,2}) $ ");
for (int i=0;i<count;i++)
{
emailpattern.matcher (temp). matches ();
}
b = System.currenttimemillis ();
System.out.println (b-a);
}
Run as 1 o'clock, run result:
4
0
When the run number is 100, run the result:
16
1
When the run number is 1000, run the result:
111
8
When the run number is 10000, run the result:
343
11
When the run number is 100000, run the result:
841
65
When the run number is 10 million, run the result:
8205
94
We know that matching regular expressions first creates a poor automaton
My conclusion: from the test demerit can be seen String.matches () method of time is not to think of linear growth, can be concluded that the JVM must be the String.matches () method cache processing, In other words, all the intermediate objects generated by this method are not cleaned by the JVM, and if applied to your application, the efficiency will be obvious.
----------------------------------------------------------------------------------------------------------
After writing the above content, still when I am complacent, I want to look at the source code of String.matches ():
Public boolean matches (String regex) {return
pattern.matches (regex, this);
}
public static Boolean matches (String regex, charsequence input) {pattern
p = pattern.compile (regex);
Matcher m = p.matcher (input);
return m.matches ();
}
It seems that my understanding of the top of the JVM is wrong,
But from their source, we can see that if you write pattern as a static property, and spring to manage objects, it will be much more efficient.
Hope for mutual encouragement.