如何在java中使用正则表达式检查URL是否包含大写字符

问题描述 投票:0回答:1

我正在尝试检查 URL 是否包含大写字符。 这是代码。

public class Test {

     public static boolean validate(String str)  
        { 
            for (char c : str.toCharArray())  
            { 
                // check if the character is not a lowercase letter 
                if (!(c >= 'a' && c <= 'z'))  
                { 
                    return false; 
                } 
            } 
            // all characters are lowercase letters 
            return true; 
        } 
      
        public static void main(String args[])  
        { 
            // test cases 
            String example1 = "alCDev-112.am.dev.oneenterprise.com"; 
            String example2 = "ABC123abc"; 
              
            // check if each example contains only lowercase letters 
            System.out.println("Example 1 contains only lowercase letters: " + validate(example1)); 
            System.out.println("Example 2 contains only lowercase letters: " + validate(example2)); 
        } 

}

但是,仅在这两种情况下它都返回 false。 请建议,我们如何使用 reg ex 来检查 java 中的 URL 不应包含大写字符

java
1个回答
0
投票

更新了检查以查找是否存在大写字符(如您的评论所述)。

public static boolean validate(String str) {
    for (char c : str.toCharArray()) {
        // check if the character is an uppercase letter
        if (c >= 'A' && c <= 'Z') {
            return true;
        }
    }
    // all characters are lowercase letters
    return false;
}
© www.soinside.com 2019 - 2024. All rights reserved.