正则表达式和Object类

来源:互联网 发布:mac如何编辑pdf 编辑:程序博客网 时间:2024/06/11 08:37

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 {
//用正则表达式判断邮箱格式     123456789@qq.com
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="bilibili@163.com";
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));
}


}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 洗手盆缝隙漏水怎么办 洗手盆裂缝漏水怎么办 洗手盆堵了怎么办 征信账号注册怎么办 注册不了征信号怎么办 阿里巴巴一键铺货到淘宝发货怎么办 淘宝购物的问题怎么办 买家评价被删除怎么办 淘宝订单虚假交易怎么办 被判定虚假物流怎么办 淘宝有虚假交易怎么办 微信辅助不了怎么办 微信验证失败怎么办 淘宝占空间太大怎么办 淘宝占用空间大怎么办 ipad空间不够用怎么办 ipadmini密码忘了怎么办 旧ipad特别卡怎么办 苹果ipad反应慢怎么办 手机垃圾多了怎么办 ipad2内存过低怎么办 苹果平板ipad内存不足怎么办 手机dns配置错误怎么办 蓝牙已停止运行怎么办 ipad看电视闪退怎么办 ipad为什么看电视会闪退怎么办 微淘直播延迟怎么办 手机淘宝进群领金币怎么办 做淘客冲销量停止淘客后怎么办 微信中零钱提现怎么办 淘宝买家不签收怎么办 小龙虾没人下单怎么办 淘宝直播不浮现怎么办 淘宝直播看不了怎么办 理财客户说没钱怎么办 投资不给钱了怎么办 工作中遇到挫折怎么办 手机qq出现异常怎么办 农行卡出现异常怎么办 淘宝长期不发货怎么办 快递一直不发货怎么办