Dark Horse programmer-Regular expressions and common other classes

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Regular Expressions:

Specifically for manipulating strings
Features: Used for some specific symbols to represent some code manipulation, thus simplifying the writing
Benefits: Can simplify writing
Disadvantages: Poor Reading

Specific features:
1. Match: Matches method
defines the number of characters and rules Reg = [Character Range] [character Range] (number of previous [] ranges)
example String reg = [Ab][a-z][0-9]{1-3} \d number
the first character (A or b) of the string representing the length of the second (A~z) third to fifth character (0~9)
boolean b=str.matches (reg) determines whether the STR string satisfies the rule reg
2. Cutting: Split Method
define the number of characters and the rule reg = "," (means to follow, cut); reg = "+" (cut by multiple spaces)
Note: multiple when plus +;. Represents any character and cannot be defined as a cut identity. It needs to be translated when needed reg= "\ \."; \ for translation \. For. The notation in the regular
\ \ 's translation is \\\\; overlapping word-cutting double-fold word reg = "(.) \\1 "(.) A group that represents any single character
Each group generation has a number \1 indicates that the first group \1 represents the same as the previous character, and is defined as a string when the modifier
3. Replace: ReplaceAll (String Reg, String replacrstr)
4. Remove
define rules, encapsulate rules for pattern objects, associate characters to get a match, find with a match, and get the found object with a matching device

Boundary Matching Device
^ The beginning of the line
End of the $ line
\b Word boundaries
\b Non-word boundaries
\a the beginning of the input
\g the end of the previous match
\z the end of the input, only for the last terminator (if any)
\z End of input

Greedy number of words
X? X, not once or once
X* X, 0 or more times
x+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n times, but no more than m times

Importjava.util.regex.*;classregexdemo{ Public Static voidMain (string[] args) {System.out.println ("Confirm QQ Number:");                 CheckQQ1 (); System.out.println ("Demo Regular Out:");        Getdemo (); System.out.println ("Demo Regular Replacement:");        Replacedemo (); System.out.println ("Demo Regular Cut:");    Splitdemo (); }    /*demand, the QQ number to verify the requirements: 5~15 bit, 0 can not start, can only be a number. */    //removed     Public Static voidGetdemo () {String str= "Ming Tian Jiu Yao Fang Jia le, da Jia"; String Reg= "\\b[a-z]{3}\\b";//\b Word boundary defines a string with a 3-letter rule, preceded by a word boundary//encapsulate a rule as an objectPattern p =Pattern.compile (REG); //Associate the regular object with the character to be played, get the matchMatcher m =P.matcher (str); //System.out.println (M.matcher ())         while(M.find ())//Boolean B = M.find ();{System.out.println (M.group ()); }    }    //Replace     Public Static voidReplacedemo () {String str= "aabbccdd00112233"; String Reg= "(.) \\1+ ";//define substitution rules to turn overlapping words into wordsString str1 = Str.replaceall (Reg, "$");//Xxxx-->x,$1 represents the first set ofSystem.out.println (STR1); }        //cut the regular notation     Public Static voidSplitdemo () {String str= "111,222,333"; String Reg= ",";//defining cut RulesString []arr = Str.split (reg);//call the Cut function         for(String S:arr) {System.out.println (s); }    }    //General Wording     Public Static voidCheckQQ1 ()//determines whether the input string satisfies the QQ number rule{String QQ= "1251111111111111aa6";//Define a string        intLen = Qq.length ();//determine its length        if(!qq.startswith ("0"))//determine if it has 0 openings        {            Char[] arr = Qq.tochararray ();//to change a string into a character array            if(! (arr.length>=5&&arr.length<=15))//determine if its length is 5~15{System.out.println ("QQ Length Error"); return; }             for(int× = 0;x<arr.length; x + +)//traversal determines whether each bit is a number            {                if(! (arr[x]>= ' 0 ' &&arr[x]<= ' 9 ') {System.out.println ("QQ can only be a number"); return; }} System.out.println ("QQ:" +QQ); }        ElseSystem.out.println ("Do not start with 0"); }}

Operation Result:

System class:

Methods and properties in a class are static.
Out: Standard output
In: Standard input
Describe some system information
Common methods:
Get System Properties Information: Properties GetProperties ();

ImportJava.util.*;Importjava.text.*;classsystemdemo{ Public Static voidMain (string[] args) {Properties prop=system.getproperties (); //because properties is a subclass of Hashtable, it is also a subclass object in the Map collection. //then the elements of the collection can be removed by means of a map. //The collection is stored in a string, which is a generic definition//How do I customize some unique information in the system? System.setproperty ("MyKey", "MyValue"); //gets the specified property information. String value1 = System.getproperty ("Os.name"); System.out.println ("Value1=" +value1); /*when JM starts, dynamically loads some property information? Java startup code: java-d Custom key = custom attribute information Class name example: Java-dmyset=myvalue2 systemdemo*/String MySet=system.getproperty ("MySet"); System.out.println ("Myset=" +MySet);  for(Object obj:prop.keySet ()) {String value2=(String) prop.get (obj); System.out.println (obj+":"+value2); }    }}

Runtime class:
The class does not provide a constructor, stating that a new object cannot be, and that the methods in that class are static.
Common methods:
EXEC ("Execute program Path") execution program

class   runtimedemo{    publicstaticvoidthrows  Exception    {         = runtime.getruntime ();         = R.exec ("C:\\Program Files (x86) \\Battle.net\\Battle.net Launcher.exe");         // P.destroy ();   // Kill the Process     }}

Date class:
The Java.text package is imported.

Calendar
Used to extract specific information from a date.

classdatedemo{ Public Static voidMain (string[] args) {Date d=NewDate ();        System.out.println (d); //Set Date formatSimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day"); String Time= Sdf.format (d);//format date format with SimpleDateFormat. System.out.println ("time =" +Time ); Calendar C=calendar.getinstance (); System.out.println (C.get (calendar.year)+ "Year" +c.get (Calendar.mouth) + "Month"); }}

Math class

public static int abs (int a): Take the absolute value of a;
public static double Ceil (double A): rounding up;
public static double floor (double a): rounded down;
public static int max (int a,int b): Take the maximum value of a and B
public static Double pow (double a,double b): Returns the value of the second argument to the power of the first parameter.
public static Double Random (): Gets a random number of type double. This value is >=0 and < 1.0
public static int round (float a): rounded rounding (self-study with double argument)
public static double sqrt (double A): Returns the positive square root of a double value that is rounded correctly

classmathdemo{ Public Static voidMain (string[] args) {DoubleD1 = Math.floor (16.34);//the floor method returns the largest integer less than the specified data.         DoubleD2 = Math.ceil (16.34);//the Ceil method returns the smallest integer greater than the specified data.         DoubleD3 = Math.Round (12.54);//Round Method Rounding        DoubleD4 = Math.pow (2,3);//Pow method The power of the data behind the preceding data        intd5 = (int) (Math.random () *10+1);//The random method generates a pseudo-random double value that is greater than or equal to 0.0 and less than 1.0. //random R = new Random (); //the second method of generating a random number. //int d5 = R.nextint (10) +1; System.out.println ("d1=" +d1+ ";d 2=" +d2+ ";d 3=" +d3+ ";d 4=" +d4+ ";d 5 =" +d5); }}

Operation Result:

Dark Horse programmer-Regular expressions and common other classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.