简体   繁体   English

Java中的特定字符串格式

[英]Specific string format in java

I want to validate a String in java that contains the following order: 我想在Java中验证包含以下顺序的字符串:

SAVA950720HMCLZL04 SAVA950720HMCLZL04

That is, four letters, six numbers, six letters and finally two numbers. 也就是说,四个字母,六个数字,六个字母以及最后两个数字。

I was reading about Regular Expressions but I can't understand how to implement it. 我在阅读正则表达式,但是我不明白如何实现它。

I did this method to validate the first four letters. 我使用这种方法来验证前四个字母。

 public void name(String s){
 Pattern pat=Pattern.compile("[a-zA-z]{4}");
 Matcher mat=pat.matcher(curp);
 if(mat.matches()){
     JOptionPane.showMessageDialog(null, "Validating");
 }else{
     JOptionPane.showMessageDialog(null, "Check your data. please", "error",JOptionPane.ERROR);
 }
 }

I think that I might be wrong because I don't know how to implement it correctly, any help about what might be the correct solution to my problem?. 我认为我可能是错的,因为我不知道如何正确实现它,关于什么是正确解决我的问题的帮助?

Regex pattern in your case would be: 您的情况下的正则表达式模式为:

[a-zA-Z]{4}[0-9]{6}[a-zA-Z]{6}[0-9]{2}

Find matching is simple: 查找匹配很简单:

public void name(String s){
    String pattern = "[a-zA-Z]{4}[0-9]{6}[a-zA-Z]{6}[0-9]{2}";
    boolean match = s.matches(pattern);
    if (match) {
        ...
    } else {
        ...
    }
}

You can test regex from here https://regex101.com/ 您可以从这里https://regex101.com/测试regex

Edit 编辑

I used [0-9] instead of \\d to ensure the safety matching for only digits. 我使用[0-9]而不是\\ d来确保仅数字的安全匹配。 More details can be found here: Should I use \\d or [0-9] to match digits in a Perl regex? 在这里可以找到更多详细信息: 我应该使用\\ d或[0-9]来匹配Perl正则表达式中的数字吗?

  1. Matcher.match attempts to match entire String against the pattern and your current pattern validates only 4 characters. Matcher.match尝试将整个String与该模式匹配,并且您当前的模式仅验证4个字符。 Here is what you can do to test you regular expression: 这是测试正则表达式的方法:

    • You can put .* at the end of regular expression to match rest of the String 您可以将.*放在正则表达式的末尾以匹配字符串的其余部分
    • You can alternatively use Matcher.find or Matcher.lookingAt to match against substring 您也可以使用Matcher.findMatcher.lookingAt与子字符串进行匹配
  2. Your pattern has a typo, second interval is too wide Az vs AZ 您的图案有错别字,第二个间隔太宽AzAZ

Try this Regex pattern: 尝试以下正则表达式模式:

[a-zA-Z]{4}\d{6}[a-zA-Z]{6}\d{2} 

The above will validate for four letters, six numbers, six letters and finally two numbers. 上面的代码将验证四个字母,六个数字,六个字母以及最后两个数字。

package com.stackoverflow._42635543;

import java.util.regex.Pattern;

public class Main {

  public static void main(String[] args) {
    Pattern pat = Pattern.compile("[A-Za-z]{4}[0-9]{6}[A-Za-z]{6}[0-9]{2}");

    if (pat.matcher("SAVA950720HMCLZL04").matches()) {
        System.out.println("match");
    } else {
        System.out.println("no match");
    }

    if (pat.matcher("SAVA950720HMCLZL045").matches()) {
        System.out.println("match");
    } else {
        System.out.println("no match");
    }
  }
}

produces 产生

match
no match

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM