简体   繁体   English

使用正则表达式验证语句

[英]validate statement using regular expression

I have written a regular expression to check the validation for the below statements 我已经写了一个正则表达式来检查以下语句的有效性

String pattern= "^how many [r|R]upees is (([A-Za-z\\s])+)\\?$";

String statment1 = "HOW MANY RUPEES IS RED BLUE GOLD ?"; // all caps
String statment2 = "how many Rupees is red blue Gold?";
System.out.println(statment1.matches(pattern));
System.out.println(statment2.matches(pattern));

Output 产量

false
true

How to validate if the statement if everything is under caps 如何验证是否一切都在上限以下的陈述

只需在模式中添加(?i)不区分大小写的修饰符即可。

String pattern = "(?i)^how many rupees is ([a-z\\s]+)\\?$";

You can try to do this without regex by using the StringUtils isAllUpperCase 您可以尝试使用StringUtils isAllUpperCase在不使用正则表达式的情况下执行此操作

Checks if the CharSequence contains only uppercase characters. 检查CharSequence是否仅包含大写字符。

!StringUtils.isAllUpperCase("HOW MANY RUPEES IS RED BLUE GOLD ?") = false
!StringUtils.isAllUpperCase("how many Rupees is red blue gold?") = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM