2017-4-23: Study notes (regular expression +api) __note

Source: Internet
Author: User
Tags dateformat string format
Regular Expression (Regex) The essence of regular expressions is a string A regular expression is used to define a matching rule that matches a sequence of strings that conform to a certain syntactic rule. in development, regular expressions are often used to retrieve and replace text that conforms to a rule matching rules for regular expressions commonly -in characters
symbols meaning
X Character X
\| Escape character
\\n Line feed
\ \ r return character
\\t Tabs
[ABC] Any one of the characters in ABC
[^ABC] Characters except ABC
[A-za-z] Any one of the characters from the A-za-z
Predefined characters
symbols meaning
[.] Any character
\\d equivalent to [0-9]
\\w equivalent to [a-za-z_0-9]
^[ABC] A string that begins at any one of ABC
$ End of Line
\b Represents a word boundary
? Appeared once or never appeared
* Occurs 0 or more times
+ Occurs one or more times
X{n} Just happens n times
X{n,} Occurs at least n times
X{n, M} Appears N to M times
Note:Letters are case-sensitive newline characters that create a blank line on the next line of the current cursor's row, and move the character after the cursor position to the next line, and the carriage return moves the cursor to the beginning of the next line \** The first slash is the escape function, the front* * to escape the * * * to the normal slash common methods for designing regular expressions in string classes Boolean matches (String regex);
"ABC". Matches ("[A]"); string [] Split (string regex);
"ABC". Split (""); To cut a string using rules string ReplaceAll (String string, string regex)
Replace a string based on the rules of a regular expression Practice cutting Strings with split ()
Package April.date23.lesson;


public class Mysplit {public
    static void Main (string[] args) {
        String [] str;
        String ori = "Abc-def-ghi-jkl-mno";
        String regex = "[-]+";
        str = ori.split (regex);
        for (String temp:str) {
            System.out.println (temp);}}}
use replaceall () to replace all numbers with the
Package April.date23.lesson;

public class Myreplace {public
    static void Main (string[] args) {
        String ori = "123456aaa123bbb456ccc1ddd33333e";
        String regex = "\\d+";
        The Ori = Ori.replaceall (Regex, "#");
        System.out.println (ori);
    }

e-mail address verification@ The number of letters underlined below 1 @ is not allowed to be less than 1. The number of letters behind is not sure
Package April.date23.lesson;
*
 * * 1234567@qq.com *
 mym_ail@sina.com *
 nimail@163.com *
 wodemail@yahoo.com.cn
/Public Class Mymatchmails {public
    static void Main (string[] args) {
        String regex = ' \\w+@[0-9a-z]+[\\.[ A-Z]] +";
         String a = "1234567@qq.com";
         String B = "mym_ail@sina.com";
         String c = "nimail@163.com";
         String d = "wodemail@yahoo.com.cn";
         System.out.println (A.matches (regex));
         System.out.println (B.matches (regex));
         System.out.println (C.matches (regex));
         System.out.println (D.matches (regex));

    }
Practical Class Date ClassThe point is millisecond value and dateThe reciprocal transformation refers to the java.util.DateIn the Date Class java.sql.Dateis in the database Date Class Millisecond Value long System.currenttimemillis ();
Returns the millisecond value of the current date Java time Origin is 1970.1.1 00:00:00corresponding millisecond value 0Dates cannot be arithmetic, so dates and times are calculated depends on the millisecond value Construction Method

Date date = new Date (); Parameterless construction method Default output system current time

Date date = new Date (long Millistime) the only parameter passed by a parameter constructor that is not obsolete is the millisecond value, and the constructor converts the millisecond value to the date CST is not the time zone, is the "Chinastanardtime" date ToString () Method has overridden the method long GetTime ();
Returns a millisecond value for the operation of a date converted to a millisecond value long settime (long millistime);
converting a millisecond value to a date construction method can also implement this function abstract DateFormat class inherits Java.text.Format and Java.text.DateFormat Language-independent definitions we often SimpleDateFormat How to format dates

Steps:
1. Create SimpleDateFormat ()
* Format of date written in construction method
2. SimpleDateFormat calls the Format () method to format dates
* String Format (date date)
* Pass Date Object , return string
Example

String format  = "yyyy year mm month DD day HH:MM:SS";
SimpleDateFormat SDF = new SimpleDateFormat (format);
String Date  = Sdf.format (new date ());
SYSTEM.OUT.PRINTLN (date);
In format keyword must be correctAnd Case SensitiveSomething other than a keyword write at random Parse MethodResolves a string to a the object of the date Date Parse (Strings)And String Format (Date)Relative

Steps:
1. Create a SimpleDateFormat object that specifies the date pattern in the constructor method
2. The subclass object invokes the method parse, passes a string, returns the date

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = Sdf.parse ("1995-5-6");
SYSTEM.OUT,PRINTLN (date);
This method throws an exception when it is used because the argument in the parse does not necessarily conform to the pattern so the class at the top of the code will throw an exception after the class with "throws Exception" Abstract Calendar ClassLanguage sensitivity, so calendar is an abstract class and the format needs to be implemented by subclasses Java.util.Calendar ConstructionNo direct new Subclass object required static GregorianCalendar getinstance ();Return value Chinese name Greenwich CLASS Method int get (int)
parameter int refers to the calendar's field parameter int calling the return value with the class name is the specific value of the Calendar field Set (int field, int value)
field is the Calendar field value is the specific value set after Date Set (int year, int month, int day)
Pass three integers year, month, day if set month to 12, date is modified to month of next year Add (int field, int value)
The offset of the calendar can specify a field in a monthly calendar Fielde, the offset of an integer value characteristicMonth Abroad is 0~11Domestic 1~12Week Abroad is 0~6Domestic 1~7

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.