开发者社区> 问答> 正文

关于java正则表达式的问题

public class RegexDemo {
public static void main(String[] args) {
String regex = "[A-Z]+_[A-Z]+{3,10}";//正则表达式  
String data = "";//要验证的字符串
System.out.println(data.matches(regex));
}
}
/*要求格式为大写字母_大写字母 总长度不能超过10
这个是我写的但是不好用 下划线只能有一个 两端的链接的为大写字母
大写字母可以1到多个 然后整体字符串长度不能超过10 求大神帮我解答一下*/

展开
收起
蛮大人123 2016-02-20 15:47:50 2202 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    单用matches()方法可能比较难,目前只想到这种方法:
    public class RegexDemo {
    public static void main(String[] args) {
    String regex = "([A-Z]+)_([A-Z]+)";//正则表达式  
    String data = "A_AAAAAAAAAA";//要验证的字符串
    Matcher m = Pattern.compile(regex).matcher(data);
    System.out.println(m.matches() && (m.group(1).length() + m.group(2).length())<=10);
    }
    }

    2019-07-17 18:45:25
    赞同 展开评论 打赏
问答分类:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载