正则表达式和Object类

Java 正则表达式

Test2.java 文件代码:

/正则表达式 regex
/*在对字符串数据进行一些复杂的匹配,查找,替换等操作时,通过正则表达式,可以方便实现字符串的复杂操作
 * 
 */
public class Test1 {
public static void main(String[] args) {


// String regex="[a-zA-Z0-9_.-]";
String regex="\\d";//0~9任意一个
String str="9";
System.out.println(str.matches(regex));//判断字符串是否匹配指定的正则表达式 true

String regex1="[0-9]";//0~9任意一个
System.out.println(str.matches(regex1));//true

String regex2="\\w";//任意单词字符,只能有一个  [a-zA-A0-9_]
System.out.println("_".matches(regex2));//true

String regex3="\\s";
System.out.println(" ".matches(regex3));//true

}


}

Test3.java 文件代码:

public class Test2 {
public static void main(String[] args) {
String regex="[a-z]?";//0<=    <=1
System.out.println("a".matches(regex));//true

String regex1="[A-Z]*";//>=0
System.out.println("ABZ".matches(regex1));//true

String regex2="[0-9]+";//>=1
System.out.println("0".matches(regex2));//true

String regex3="[a-zA-Z0-9]{3}";//==3
System.out.println("aA0".matches(regex3));//true

String regex4="[a-zA-Z0-9._-]{3,}";//>=3
System.out.println("aA0-".matches(regex4));//true

String regex5="[ace]{2,4}";//2<=   <=4
System.out.println("ac".matches(regex5));//true
}


}

正则表达式邮箱判断

下面是一个例子:

Test4.java 文件代码:

public class Test3 {
//用正则表达式判断邮箱格式     [email protected]
public static void main(String[] args) {
// TODO Auto-generated method stub
// String str=".";任意字符
// System.out.println("a".matches(str));
String emailRegex="^[a-zA-Z0-9_.-]+@([a-zA-Z0-9_.-]+\\.)+[a-zA-Z0-9]{2,4}$";//任意一个
String email="[email protected]";
System.out.println(email.matches(emailRegex));

}


}

String字符串的拆分

//字符串的拆分  split()   可以将字符串,按照特定的分隔符拆分成字符串数组
public class Test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="java cpp php c# objective-c";
String regex="\\s";//用空格进行拆分
String[] array=str.split(regex);
System.out.println("array="+Arrays.toString(array));//array=[java, cpp, php, c#, objective-c]

String str1="100+200-150=150";
String[] array1=str1.split("[\\+\\-\\=]");
System.out.println("array1="+Arrays.toString(array1));//array1=[100, 200, 150, 150]
}


}

Cell类重写Object的toString()和equals()方法

public class Cell extends Object{
private int row;//行
private int col;//列
public Cell(){
super();
}
public Cell(int row, int col) {
super();
this.row = row;
this.col = col;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public void print(){
System.out.println("row="+this.row+",col="+this.col);
}
//Cell类继承了Object类可以重写Object类里的equals()方法

@Override
public boolean equals(Object obj) {
if (this == obj){
return true;
}
if (obj == null){
return false;
}
if (getClass() != obj.getClass()){
return false;
}
Cell other = (Cell) obj;
if (col != other.col){
return false;
}
if (row != other.row){
return false;
}
return true;
}



// @Override
// public boolean equals(Object obj){//cell.equals(cell1);  this=cell  obj=cell1
// //地址一样表示同一个对象
// if(this==obj){
// return true;
// }
// if(obj==null){
// return false;
// }
// //表示同一点
// if(obj instanceof Cell){//表示obj是Cell的子类或同类
// Cell cell1=(Cell) obj;//向下造型  Object obj转成Cell类型
// return this.row==cell1.row&&this.col==cell1.col;
// }
// return false;
// }
//Cell类继承了Object类可以重写Object类里toString()
public String toString() {
return "Cell [row=" + row + ", col=" + col + "]";
}

}

Cell类的测试类

public class TestCell {
public static void main(String[] args) {
Cell cell=new Cell(3,4);
//cell.print();
System.out.println("cell="+cell);//toString()默认存在

Cell cell1=new Cell(3,4);
System.out.println("cell1="+cell1);
System.out.println(cell==cell1);//false  ==比较的是地址

System.out.println(cell.equals(cell1));
}


}

猜你喜欢

转载自blog.csdn.net/Anthonybuer/article/details/70245170