用replaceAll去掉身份证中的特殊符号

本文介绍了一种处理身份证号码的方法,通过正则表达式去除特殊字符,保留数字和字母X,确保了身份证号码的有效性和规范性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//去掉特殊符号 如500213719851119037X# 去掉# (保留X)

cardNo = cardNo.replaceAll("[^\\d|X]", "");


最后cardNo = 500213719851119037X
public List<ValidationResult> compareContent2(String pdfText, JsonNode jsonConfig) { List<ValidationResult> results = new ArrayList<>(); // 使用HanLP对文本进行分词 List<String> pdfTokens = HanLP.segment(pdfText).stream() .map(term -> term.word.replaceAll("\\s+", "").toLowerCase()) .collect(Collectors.toList()); System.out.println("分词的效果:"+pdfTokens); // 处理JSON结构对象/数组 JsonNode dataNode = jsonConfig.isArray() && jsonConfig.size() > 0 ? jsonConfig.get(0) : jsonConfig; // 高效遍历JSON字段 dataNode.fields().forEachRemaining(entry -> { String key = entry.getKey(); String value = entry.getValue().asText(); // 使用HanLP对JSON值进行分词 List<String> valueTokens = HanLP.segment(value).stream() .map(term -> term.word.replaceAll("\\s+", "").toLowerCase()) .collect(Collectors.toList()); if (!valueTokens.isEmpty()) { boolean found = pdfTokens.containsAll(valueTokens); results.add(new ValidationResult( "FIELD", key, String.join("", valueTokens), found ? "Found" : "Not Found", found )); } }); return results; }这个我的比较校验的方法这个是我的方法 public String extractContent(MultipartFile file) { String contentType = file.getContentType(); String fileName = file.getOriginalFilename().toLowerCase(); if (contentType == null) { return "不支持的文件类型: " + contentType; } if (fileName.endsWith(".pdf")) { return readPdfText(file); } return extractImageText(file); } /** * 读取PDF文本内容 * * @param file * @return */ public String readPdfText(MultipartFile file) { try (PDDocument doc = PDDocument.load(file.getInputStream())) { PDFTextStripper stripper = new PDFTextStripper(); // 设置行分隔符 stripper.setLineSeparator("\n"); // 设置字符间距 stripper.setSortByPosition(true); String rawText = stripper.getText(doc); System.out.println("pdf内容" + rawText); return rawText.trim(); } catch (Exception e) { return MessageUtils.message("file.read.pdf.error"); } } @PostConstruct public void initOcrEngine() { tesseract = new Tesseract(); //语言包路径和支持语言 tesseract.setDatapath("D:\\maven_use\\lingxi-lhc\\lingxi-ai-extend\\lingxi-ai-comparison\\src\\main\\resources\\tessdata"); tesseract.setLanguage("eng+chi_sim"); tesseract.setPageSegMode(6); //自动页面分割 tesseract.setOcrEngineMode(1); //LSTM引擎 } /** * OCR识别图片内容 */ private String extractImageText(MultipartFile file) { try (InputStream is = file.getInputStream()) { BufferedImage image = ImageIO.read(is); if (image == null) { return MessageUtils.message("Image.parsing.failed"); } //OCR识别 String result = tesseract.doOCR(image); result = postProcess(result); result = result.replaceAll("\\s+", " ").trim(); System.out.println("图片内容:\n" + result); return result; } catch (Exception e) { e.printStackTrace(); return MessageUtils.message("file.read.picture.error"); } } 我的pdf没有问题 我的ocr比较校验有问题
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值