JAVASE01---Unit02: regular expression, Object, wrapper class

Source: Internet
Author: User
Tags gety wrapper java se

The regular expression is not a Java thing, it is an independent set of systems, it can describe the rules of the string, the format of the rules, general matching. Java writes a set of libraries to support regular expressions, and other languages can use regular expressions, so it's not a Java patent.

Java Core API
Java SE day02
****************************************************************************
API accepted.

Development tools:
Ctrl+f Find
Regular Expressions Regular Expression
Find:string (The simplest regular, string)
Case sensitive casing is strictly differentiated (ticked) in principle a case-sensitive
Find a conversation with a number variable on the start of S
FIND:S[0123456789] or s[0-9] or s\d
The syntax provided by the regular: s---the first character is s;
[]---A character, character set, in which the square brackets are present;

Test Character Set
String provides, can check whether the preceding string conforms to this regex1 regular rule?
Matches

****************************************************************************
Package day02;

public class regdemo01{
public static void Main (string[] agrs) {
String reg= "[ABC]";
/*
* Java String API method matches ()
* Used to test whether the whole string conforms to the regular rule:
* check if "a" complies with [ABC] rules
*/
Boolean b= "a". Matches (reg);
System.out.println (b); True

Reg= "s\\d"; The front bar is the transfer character "\" s[0-9] s[0123456789]
System.out.println (REG); S\d
b= "S1". Matches (reg);
System.out.println (b); True

Reg= "s\\s";
B= "S". Matches (reg);
System.out.println (b); True
b= "SB". Matches (reg);
System.out.println (b); False

B= "S\t". Matches (reg); T-tab
System.out.println (b); True

B= "s\n". Matches (reg); N-Whitespace characters
System.out.println (b); True

}
}
****************************************************************************
Zip Code: (6 numbers)
[0-9] [0-9] [0-9] [0-9] [0-9] [0-9]
\d\d\d\d\d\d
\D{6}---Number of words
S{6}
\w{8,} No Limit
\w{8,10} at least 8, up to 10


****************************************************************************
Package day02;

public class regdemo02{
public static void Main (string[] agrs) {
/*
* Regular expression number words
* {n} represents n
* {n,} at least n
* {n,m} at least n a maximum of M
* \d{6}:6 Digits
* \w{8,}: Minimum of 8 word characters
* \w{8,10}: 8~10 of Word characters
* S{6}:6 a S
*/
String reg= "\\d{6}"; \D{6}
Boolean b= "650201". Matches (reg);
System.out.println (b); True

B= "650000". Matches (reg);
System.out.println (b); False

String reg= "\w{8,}";
int c= "Yangrong". Matches (reg);
System.out.println (c); //--------------------?
}
}
****************************************************************************
Package day02;

public class regdemo03{
public static void Main (string[] agrs) {
/*
* Simplified numerals
* ? Equivalent to {0,1}
* * Equivalent to {0,}
* + equivalent to {1,}
*
Case
* s? No, or there's 1 of S.
* S* No or there's a lot of s
*/
String reg= "s?";
Boolean b= "S". Matches (reg);
System.out.println (b); True

B= "". Matches (reg);
System.out.println (b); True

b= "SS". Matches (reg);
System.out.println (b); False
}
}

****************************************************************************
01:41:35

1703 This account is sealed, the following is Fan speaking

Review: jsd1705
Strings are immutable objects, Chang, and Java is based on a constant pool mechanism that allows us to reuse string objects to reduce the use of memory overhead for strings.
However, because forcing us to reuse objects when we use literals, the objects become public, so objects are immutable and content cannot be changed. To change it, you must create a new object.

Frequently created objects, string performance can be poor. Class: StringBuilder


string supports expression-related methods
What is a regular expression? Not a Java thing, someone has a unique system, perfect things. Java provides methods to support the use of regular expressions, and other languages can also be used.

An expression consisting of a bunch of characters, which is used to describe the format of a string. A tool for formatting matching validation strings.

****************************************************************************

AM string supports regular expressions

The regular expression is not a Java thing, it is an independent set of systems, it can describe the rules of the string, the format of the rules, general matching. Java writes a set of libraries to support regular expressions, and other languages can use regular expressions, so it's not a Java patent.
Because it has a separate system, it is relatively complex. The purpose of our learning of regular expressions is to be able to read and to write basic regular expressions is enough.

The first reaction of the human brain, the format of a mailbox should be what, should be preceded by a user name, there is a @ in the middle, there is a domain name behind. com.

Should we not impose a range that does not belong to the regular expression?
Regular expressions do not pay attention to validity, only the format is correct. The specific value is not concerned.

Regular expressions
One, the character set:
[] square brackets: denotes a character, in square brackets you can enumerate what this character is.
[ABC]: This is a character, and this character is either a, B, C, can only be one of them, others do not.
[^abc]:^ Non-
[A-z]:-to
[a-za-z0-9]: Can be case, can be a number, can only represent one of them.
[A-z&&[^bc]]:a to Z, but does not contain B, c=[ad-z]

Second, the reservation of the semantic character set:
. Any character (a space is counted)
\D=[0-9]
\W=[A-ZA-Z0-9_] numbers, letters, underscores
\S=[\T\N\XOB\F\R] Any white space character, the placeholder invisible Java with trim can be removed.
Uppercase and lowercase inverse relationships (keep lowercase in mind)
\d
\w
\s

Third, the number of words:
X?? represents 0 or 1 x; indicates that the preceding x appears either there or not. Absolute
For example: [A-z]? ---Two lowercase letters will not work.
x* * denotes 0 or any number of x;
For example: [a-z]*---any number of letters, but not with numbers.
X+ + represents 1 to any number of x (greater than or equal to 1 x)----More than once, one does not write.
Determined quantifiers:
X{n} represents n x
X{n,} represents n to any number of x (greater than or equal to n x)
X{n,m} represents n to M x

[ABC]?
[abc]*
[abc]+ must write at least once
[ABC] {3,} more than 3 times
[ABC] {3,5} 3 to 5 times
Up to three times
[ABC] {0,3} does not write negative numbers, the minimum value is 0

Iv. grouping "()":
"()": The contents of the inside as a whole.
[ABC] {3}
Abb
Abc
Acc
(ABC) {3}
Abcabcabc

(ABC|DEF) {3}
Abcdefabc


e-mail address:
[Email protected]
User name @ domain name

User name: number, letter, underline
[a-za-z0-9_]+: The plus sign indicates that at least one of the preceding items is written.

@: Just @, be sure to die. It is not necessary to write [@],[] to denote a character, which is what is allowed.

Domain name: Numbers, letters, underscores

. cn/.com:. (cn|com|...tt|.com.cn)
Dot What: (\.[ a-za-z]+)

*************************************************************************
Package day02;
/**
* Regular Expression:
* Regular expression is a set of special characters described by an expression, the format of a string,
* Then use this expression to verify that a string conforms to the format requirement.
*
* String supports one of the related methods of regular expressions:
* Boolean matches (String regex)
* This method verifies that the current string satisfies the format requirement with the given regular expression and returns true if satisfied.
* It is important to note that the regular expression does not add a boundary match (^...$), but also does a full match validation.
* @author Daisy.yang
*/
public class string_mathes{

public static void Main (string[] args) {
String email= "[email protected]";
/**
Regular Expressions for *email:
*[a-za-z0-9_.] [Email protected] [A-za-z0-9]+ (\.[ a-za-z]+) +
*
*\\
*string regex= "\\\\"; The regular expression is: \ \
*/
String regex= "[a-za-z0-9_\\.] [Email protected] [A-za-z0-9]+ (\\.[ a-za-z]+) + ";
System.out.println (regex);

Boolean match=email.matches (regex);
if (match) {
System.out.println ("is the email address! ");
}else{
System.out.println ("Not an email address! ");
}

}

}

**********************************************************************
**********************************************************************
Package day02;
/**
* string[] Split (String regex)
* Splits the current string according to the portion of the given regular expression that can be matched.
* Save several pieces of string after splitting into an array and return.
* @author DAISY.Y
* Splits the part of the current string that satisfies the given regular expression,
* Return all remaining parts as an array.
* @author Student
*
*/
public class String_split {

public static void Main (string[] args) {
String str = "Abc123def456ghi789uio";
string[] data = Str.split ("\\d+");
String[] word= str.split ("[a-z]+");
System.out.println ("Split the +word.length+" item: "); 4 items include an empty string
If "[A-z]" There are 10 items. At the end of a continuous match, the empty string is not. But all the previous needs.
/*
* \\d represents a number;
* \\d+ represents a continuous number part (+ indicates more than once);
*/
Str.split ("[0-9]");
for (int i=0;i<word.length;i++) {
System.out.println (Word[i]);
}

}

}

*************************************************************************
*************************************************************************
Package day02;
/**
* Image renaming:
* Upload pictures on the internet, the name of the image has been changed. Gif
* @author Daisy.yang
*
*/
public class String_split2 {
public static void Main (string[] args) {
String imgname= "123.jpg";
/*
* Piling: First output look right?
* "." Represents any one character in a regular expression.
* The output result is 0.
*/
String[] names= imgname.split ("\ \");
System.out.println (names.length); 0

Imgname= System.currenttimemillis () + "." +NAMES[1];
System.out.println (imgname);
}

}
**************************************************************************
**************************************************************************
Package day02;
/**
* String ReplaceAll (String regex,string str)
* Replace the part of the current string that satisfies the regular expression with the given string
* @author DAISY.Y
*
*/
public class String_replaceall {

public static void Main (string[] args) {
String str= "aaa123bbb456ccc789ddd";
/*
* Replace the number part with "#NUMBER #"
* [0-9]: Each number is num once.
* [0-9]+: If the number is not Gaga, it will be num once for each number.
*
* [0-9].:aaa#num# #NUM #bb#num# #NUM #cc#num# #NUM #dd
* (There is a problem, wood has this type O (∩_∩) o haha ~)
*/
Str= Str.replaceall ("[0-9].", "#NUM #");
System.out.println (str);
/*
* Replace the letter part with "#CHAR #"
*/
//str=str.replaceall ("[a-z]+", "#CHAR #");
}

}
***********************************************************************
REPLACEALL2:
When playing the game, it will be used. Say, a little too drastic. The server helps you in harmony.
Server has a very large regular expression library, will be used in the bad language statistics, all the words come over
ReplaceAll again to the other side to see.
GM at work, staring at someone to report to you, in the Harmonious language library and add a curse of the new language.
Regular expressions are dynamically spliced.
Wait for the next boot to be harmonious!
***********************************************************************
Package day02;
/**
* Harmonious language
* @author DAISY.Y
*
*/
public class String_replaceall2 {
public static void Main (string[] args) {
/*
* () Parentheses: phrase, middle content, | or.
*/
String regex= "(s13|w q N m L g B|NC|CNM|MDZZ|DJB|SB|NMLGB)";
String message= "wqnmlgb! you this djb! how can you so nc!cnm!s 13";

Message=message.replaceall (Regex, "* * *");
SYSTEM.OUT.PRINTLN (message);
}

}
**********************************************************************
Package day02;

Import Java.util.Scanner;

/**
* After the program starts, asks the user to enter the e-mail address,
* Then verifies that the mailbox format is correct, if the output is incorrect:
* The format of the mailbox you entered is incorrect, please retype it.
* When the input is correct, output a sentence:
* Hello! [user], you use a mailbox domain name of [host]
*
* For example: [email protected]
* Output: Hello! FANCQ, you are using a mailbox domain name of tedu.
* @author Student
*
*/
public class Test {

public static void Main (string[] args) {
Scanner scanner= New Scanner (system.in);

String email= null;
while (true) {
System.out.println ("Please enter your e-mail address:");
Email= Scanner.nextline ();
if (matches (email)) {
Break
}
System.out.println ("The mailbox you entered is not in the correct format!) ");
}
Get user name: [email protected]
String[] Data= email.split ("@");
String username= data[0];
tedu.cn
String hostname= data[1].substring (0,data[1].indexof ("."));
SYSTEM.OUT.PRINTLN ("Hello! "+username+", you use the domain name of the mailbox is: "+hostname);
}
/**
* Verify that the given mailbox meets the requirements of the format
* @param Email
* @return
*/
public static Boolean matches (String email) {
/*
* "[a-za-z0-9_\\.] [Email protected] [A-za-z0-9]+ (\\.[ a-za-z]+) + "
*/
String regex= "[a-za-z0-9_\\.] [Email protected] [A-za-z0-9]+ (\\.[ a-za-z]+) + ";
return email.matches (regex);
}

}
***********************************************************************
Pm

Object
All classes in Java are subclasses of object, in addition to its own (hard indicator requirements).
are divided into two types: direct or indirect.

Three features of Java: Encapsulation, inheritance, polymorphism.

Polymorphism: Multiple forms, behaviors, and objects polymorphism. The receiving subclass of the parent class is polymorphic.


Which of the subclasses and the parent class is first?
A: There are subclasses first.
The world's first tigers, cats, leopards, lions, humans only to find that they have a common feature, the abstraction of something called: Cat animals.
There is no cat, Tiger, lion when the cat is provided. Yes, it is!
That's why humans abstract a thing called a cat animal.
The abstract abstraction is finally to the top level. The more the top layer the less the pyramid.

Introduction to other class libraries in Java
Object (object, Thing) all objects, all things.

Class (Properties, Methods)
Attributes: Features, distinctions, differentiates you from me
Method: Behavior, what would you do?

In the future, when we write the program, we will not think of the parent class and then think of the subclass, exactly the reverse.
Think of the subclass first, then the parent class. Just like in real life, there are many things to see, to begin to summarize, to abstract out the parent class.


Refactoring Code:
First, the function is realized, in the reconstruction of the time to summarize, between the class has what common, and then proposed the parent class.

The more you go to the parent class, the fewer methods.
Java is cautious when defining the object method.
******************************************************************************
Package day02;
/*
* Normal will write extends, here do not write anything. However, the default is that object has already been inherited.
* Just the source code is not, the compiler when you compile to a class file.
* All classes in Java are subclasses of object, except for themselves (hard indicator requirements).
* Why???????????????????????
*
*/
public class Demo extends object{

public static void Main (string[] args) {
Object o= new Demo ();
O.tostring ();
}
}
*******************************************************************************
Package day02;
/**
* Your wallet, age is a private property.
* Private properties: not visible, want to ask me.
* Private outsiders can not see, in the want to quote someone's purse, quote is not.
* @author Student
* attribute is private and behavior is public.
*
*/
public class Person {
private int age;

public void Setage (int.) {
if (age>100| | age<0) {
Return
}
This.age=age;
}

public int getage () {
return age;
}

}
***************************************************************************
Package day02;

public class Testperson {

public static void Main (string[] args) {
Person p= new Person ();
P.setage (10000);
System.out.println (P.getage ());
}
}
**************************************************************************

Gets the set, get
White space: Right mouse button--->source--->generate Getters and Setters ...---properties of the > class Select All

**************************************************************************
Package day02;
/**
* All classes in Java inherit from object
* When we define a class that does not inherit any class using extends, the compiler
* The current class is automatically inherited from object at compile time
*
* Use this class to test the method of overriding object
* @author Daisy.yang
*
* Points on the Cartesian point: X, y;
* Attribute privatization, behavior in public.
*
*/
public class Point {
private int x;
private int y;

public int GetX () {
return x;
}
public void SetX (int x) {
this.x = x;
}
public int GetY () {
return y;
}
public void sety (int y) {
This.y = y;
}

}


****************************************************************************
Package day02;
/**
* Test point override Object-related methods
* @author DAISY.Y
*
*/
public class TestPoint {
public static void Main (string[] args) {
Point p= new Point ();
P.setx (1);
P.sety (2);

String str= p.tostring ();
System.out.println (str); [Email protected] address in the heap

}
}
*****************************************************************************
How to rewrite tostring????
*****************************************************************************
Package day02;
/**
* All classes in Java inherit from object
* When we define a class that does not inherit any class using extends, the compiler
* The current class is automatically inherited from object at compile time
*
* Use this class to test the method of overriding object
* @author Daisy.yang
*
* Points on the Cartesian point: X, y;
* Attribute privatization, behavior in public.
*
*/
public class Point {
private int x;
private int y;

public int GetX () {
return x;
}
public void SetX (int x) {
this.x = x;
}
public int GetY () {
return y;
}
public void sety (int y) {
This.y = y;
}
/**
* When you need to use the ToString method of a class, you should
* Override this method. Because object provides the string that the method returns
* is the handle of the object (address information)
*
* The string returned by the ToString method should contain the current object's
* Attribute information. The exact format depends on the actual requirements.
*/
Public String toString () {
Return "(" +x+ "," +y+ ")";
}
/**
* The function of the Equals method is to compare the object given by the current object with the argument
* content is consistent. That is: Two objects "not like"
*
* If this method is not overridden, it is provided with object, but the object
* The internal comparison principle provided by the Equals method is "= =", so it has no practical meaning.
*
* The Equals method does not require that all property values for both objects must be exactly the same
* Define criteria for the actual business logic requirements.
*/
public boolean equals (Object obj) {
if (obj==null) {
return false;
}
if (obj==this) {
return true;
}
if (obj instanceof point) {
Point p= (point) obj;
Return this.x==p.x&&this.y==p.y;
}
return false;
}

}


***********************************************************************
Package day02;
/**
* Test point override Object-related methods
* @author DAISY.Y
*
*/
public class TestPoint {
public static void Main (string[] args) {
Point p= new Point ();
P.setx (1);
P.sety (2);

String str= p.tostring ();
System.out.println (str); [Email protected] address in the heap

/*
* SYSTEM.OUT.PRINTLN (Object obj)
* This method returns the value of the ToString method of the given object
* Output to the console
*/
SYSTEM.OUT.PRINTLN (P);

Point p2= new Point ();
P2.setx (1);
P2.sety (2);
System.out.println (p2);

System.out.println (P==P2); //---??? False
System.out.println (P.equals (p2)); //---??? False??? is provided by object. ---True
}
}
**************************************************************************
Equals method:
equals in the string is the content, and the difference between equals and = = is here.
The code above is also said.
***************************************************************************
2:05 basic types have a problem:
Integerdemo.java
**************************************************************************

JAVASE01---Unit02: regular expression, Object, wrapper class

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.