Java编写代码“邮箱格式”等进行验证。(假设邮箱以数字或是字母开头,必须包含@字符,并以.com结尾)

第一种方法

我最先想到了办法,这种方法就比较简单了,话不多说看代码

if(inputname.contains("@") && inputname.endsWith(".com") &&
				((inputname.charAt('0')>=0&&inputname.charAt(0)<='9')||
						(inputname.charAt(0)<='z'&&inputname.charAt(0)>='A'))){
			System.out.println("是emali格式");
		}else{
			System.out.println("格式错误!");

当时我犯了一个特别特别重要的错误,导致结果出错,因为是String型的,首字母也一定是字符型,我忘记了引号,导致代码一直出错,不加引号不会判断出首字母是数字!错误运行结果正确的应该是这个样子!!!
正确运行结果

第二种方法

刚刚接触Java,面向对象的思想理解的不是很透彻,我也尽力再用这种思想写。

  • 判断首字母是用正则表达式进行的(小白的我查阅了很多资料)
  • 其他的就很简单啦,都是最简单的方法
package hrbxy.may11;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailName {

	public static String inputEmai;

	public static String getInputEmai() {
		return inputEmai;
	}

	public static void setInputEmai(String inputEmai) {
		EmailName.inputEmai = inputEmai;
	}
}

package hrbxy.may11;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

    public class EmailNameTest {

        //判断是否以数字或字母开头
        public static boolean isStartWithChar(String str){
            Pattern pattern = Pattern.compile("^[0-9a-zA-Z].*");
            Matcher isChar = pattern.matcher(str.charAt(0) + "");
            if (!((Matcher) isChar).matches()) {
                return false;
            } else {
                return true;
            }
        }

        //判断是否包含@
        public static boolean iscontain(String str){
            if(str.contains("@")){
                return true;
            }
            return false;
        }

        //判断是否以.com结尾
        public static boolean isEndWith(String str){
            if(str.endsWith(".com")){
                return true;
            }
            return false;
        }

}

package hrbxy.may11;

import java.util.Scanner;

public class EmailNameMain {
    /*对“邮箱格式”等进行验证。(假设邮箱以数字或是字母开头,必须包含@字符,并以.com结尾)*/
    public static void main(String[] args) {

        EmailNameTest test = new EmailNameTest();
        EmailName name = new EmailName();

        Scanner input = new Scanner(System.in);
        System.out.print("请输入邮箱号码:");
        String inputname = input.nextLine();
        name.setInputEmai(inputname);

        boolean isNumChar = test.isStartWithChar(name.getInputEmai());
        boolean iscontain = test.iscontain(name.getInputEmai());
        boolean isEndWith = test.isEndWith(name.getInputEmai());

        if(isNumChar&&iscontain&&isEndWith){
            System.out.println("Email格式正确");
        }else{
            System.out.println("格式错误!");
        }
    }
}
附加正则表达式(只是此问题用到的)
Pattern pattern = Pattern.compile(regex);     //创建一个Pattern对象
Matcher matcher = pattern.matcher(string);      //得到Matcher对象,通过find方法可以找到匹配对象,如果有匹配的部分返回真,使用m.group方法得到匹配的各组值,否则find方法返回false。
后知后觉

写文章是为了使自己更透彻的了解,希望看到的能给我提一些建议,让我能够写得更好

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用Apache POI库来读取Excel表格,并使用正则表达式匹配某一列的单元格内容。以下是一个例子代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelParser { public static void main(String[] args) throws IOException { String fileName = "example.xlsx"; // Excel文件名 int columnIndex = 2; // 要匹配的列的索引,从0开始计数 String pattern = "^\\w+\\d+$"; // 匹配模式,字母开头数字结尾字符串 FileInputStream file = new FileInputStream(fileName); Workbook workbook = new XSSFWorkbook(file); Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 Pattern regex = Pattern.compile(pattern); for (Row row : sheet) { Cell cell = row.getCell(columnIndex); if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) { String value = cell.getStringCellValue(); Matcher matcher = regex.matcher(value); if (matcher.matches()) { System.out.println(value); } } } workbook.close(); file.close(); } } ``` 这个例子会读取Excel文件中第一个工作表的每一行,提取指定列的单元格内容,并使用正则表达式匹配符合要求的字符串。如果找到符合要求的字符串,就会输出它们的值。你可以根据需要修改文件名、列索引和匹配模式等参数。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值