0

Sorry for being new to programming. Apparently I'm expected to know everything about the topic before posting here. I've only been doing this 3 weeks, learning as I go.

I've got a field limited to 5 characters I'm trying to validate. The first character must be a letter, and the following 4 characters must be numbers. Regex is like Greek to me at this point, so I'm having trouble. I've been able to get the first character validated, but I'm stumped on the remaining 4. Here's my code:

if (carID.substring(0, 1).matches("[0-9]")) {
    showDataFormatError();
    break;
} else {
    if (carID.substring(1, 5).matches("[a-zA-Z]")) {
    showDataFormatError();
    break;
    }
}

Updating to demonstrate my horrible coding at this point.

            if (carID.length() < 5) {
                showDataLengthError();
                break;
            } else {
                if (carID.matches("^[a-zA-Z][0-9]{4}$")) {
                    showDataFormatError();
                    break;
                } else {
                    if (carYearString.length() < 0) {
                        showDateLengthError();
                        break;
                    } else {
                        try {
                            int carYear = Integer.parseInt(carYearString);
                            int currentYear = Calendar.getInstance().get(
                                    Calendar.YEAR);

// etc.
6
  • 1
    Try carID.matches("^[A-Za-z]\\d{4}$"). Commented Aug 29, 2013 at 4:49
  • You can learn basics of regex from here and here Commented Aug 29, 2013 at 4:50
  • @falsetru doesn't seem to work at all. Commented Aug 29, 2013 at 4:57
  • @geek_sauce, See ideone.com/RxXXsh Commented Aug 29, 2013 at 4:59
  • 1
    You are calling showDataFormatError() when the data is correct. You need if (!carID.matches ... Commented Aug 29, 2013 at 5:18

3 Answers 3

2

Try this

boolean valid = carID.matches("^[a-zA-Z][0-9]{4}$");

And see the tutorial

EDIT thanks to the down voter I spotted and fixed the error.

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

1 Comment

Can you give an example? ` String carID = "a1234"; boolean valid = carID.matches("^[a-zA-Z][0-9]{4}$"); System.out.println("carID "+valid); ` outputs ` carID true `
2

If you want to make sure the entire string is that format, use:

^[a-zA-Z]\d{4}$

this will help you and for tutorial you should have to go through this link

Regular Expression

Comments

1

I would do something like..

\pL       Matches Any kind of letter from any language.
\pN{4}    Matches Any kind of numeric character in any script. (4 times) greedy.

...

if (carID.length() > 5) throw IllegalArgumentException();

if (carID.matches("^\pL\pN{4}$")){
  return true;
} else {
  return false;

  ...
}

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.