user2078308
user2078308

Reputation: 41

How do I find out if string contains multiple letters/characters using regex?

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?

Upvotes: 0

Views: 191

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370679

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

Pattern pattern = Pattern.compile("[A-Z][^A-Z]*[A-Z]");

Upvotes: 1

Related Questions