java - Regular Expression for UpperCase Letters In A String

Java - Regular Expression for UpperCase Letters In A String

To check if a string contains only uppercase letters using a regular expression in Java, you can use the following pattern:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class UppercaseLettersValidation { public static void main(String[] args) { String input = "HELLO"; boolean containsOnlyUppercase = containsOnlyUppercaseLetters(input); System.out.println("Does the string contain only uppercase letters? " + containsOnlyUppercase); } public static boolean containsOnlyUppercaseLetters(String input) { // Check if the string contains only uppercase letters String regex = "^[A-Z]+$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); return matcher.matches(); } } 

In this example:

  • ^[A-Z]+$ is the regular expression.
    • ^ asserts the start of the string.
    • [A-Z] allows only uppercase letters.
    • + allows one or more occurrences of the preceding pattern.
    • $ asserts the end of the string.

The containsOnlyUppercaseLetters method returns true if the input string contains only uppercase letters; otherwise, it returns false.

Adjust the input input as needed. If the input string contains only uppercase letters, the output will be:

Does the string contain only uppercase letters? true 

Modify the regex or adjust the code based on your specific requirements.

Examples

  1. Java Regex to Check if a String Contains Uppercase Letters:

    String input = "Contains Uppercase Letters"; String regex = ".*[A-Z].*"; 
  2. Validate Uppercase Letters at the Beginning of a String in Java:

    String input = "UpperCase At Start"; String regex = "^[A-Z].*"; 
  3. Java Regex to Validate Only Uppercase Alphabets:

    String input = "ONLYUPPERCASE"; String regex = "^[A-Z]+$"; 
  4. Check if a String Ends with Uppercase Letters in Java:

    String input = "EndsWithUppercase"; String regex = ".*[A-Z]$"; 
  5. Java Regex to Count Uppercase Letters in a String:

    String input = "Count UPPERCASE Letters"; String regex = "[A-Z]"; 
  6. Validate Uppercase Words in a String with Java Regex:

    String input = "All UPPERCASE WORDS"; String regex = "\\b[A-Z]+\\b"; 
  7. Java Regex to Allow Only Uppercase Alphabets and Spaces:

    String input = "UPPERCASE AND SPACES"; String regex = "^[A-Z\\s]+$"; 
  8. Validate Uppercase Letters Between Digits in Java:

    String input = "123UPPER456"; String regex = "\\d+[A-Z]+\\d+"; 
  9. Java Regex to Match Uppercase Letters Except in Parentheses:

    String input = "(No Uppercase Here)"; String regex = "[^(][A-Z]+[^)]"; 
  10. Check if All Letters in a String are Uppercase in Java:

    String input = "ALLUPPERCASE"; String regex = "^[A-Z]+$"; 

More Tags

windows-networking export aspose autostart ios4 discount kendo-ui intel-mkl django-testing value-initialization

More Programming Questions

More Date and Time Calculators

More Math Calculators

More Chemistry Calculators

More Mixtures and solutions Calculators