0

In Java, is there a way to find out if string contains multiple letters/characters using Regular Expressions/ Pattern matching?

I have tried to solve my problem using below code

      String id = "A12B45";

      Pattern pattern = Pattern.compile("[A-Z]*");
      Matcher matcher = pattern.matcher(id);

      if (matcher.find()) 
              {
          System.out.println("YES---");
      } else 
              {
          System.out.println("NO---");
      }

The above code is not giving me an output which I want. When string contains more than one alphabet letter then it should return "YES---". Can you please help?

2
  • Do you mean you want to print YES--- when the string contains more than one letter consecutively? For example AK12B45 should print YES--- (because it contains A followed immediately by K) but A12B45 should print NO--- ? Commented Aug 9, 2019 at 6:29
  • Your question is not clear..What do you mean by 'string contains more than one alphabet letter ' is it anywhere in the string or consecutive occurrence? Commented Aug 9, 2019 at 6:41

1 Answer 1

1

Match an alphabetical character, then anything but alphabetical characters, then an alphabetical character again:

Pattern pattern = Pattern.compile("[A-Z][^A-Z]*[A-Z]");
Sign up to request clarification or add additional context in comments.

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.