0

I want to validate a String in java that contains the following order:

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?.

2
  • 1
    Note that [A-z] matches more than just letters. The matches() method anchors the regex, so you can only match strings of 4 chars. So, you need matches("[A-Za-z]{4}[0-9]{6}[A-Za-z]{6}[0-9]{2}") Commented Mar 6, 2017 at 21:09
  • A-z should be A-Z. To represent digits use [0-9] or \d (in String literals you need to additionally escape \ so it will look like "\\d"). Also matches() checks if regex can match entire string, not just part of it. Commented Mar 6, 2017 at 21:10

4 Answers 4

2

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/

Edit

I used [0-9] instead of \d to ensure the safety matching for only digits. More details can be found here: Should I use \d or [0-9] to match digits in a Perl regex?

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Matcher.match attempts to match entire String against the pattern and your current pattern validates only 4 characters. 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
  2. Your pattern has a typo, second interval is too wide A-z vs A-Z

Comments

0
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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.