Java-如何验证此字符串?
Java中是否存在执行以下此类任务的工具 我得到了这个硬输入字符串:{[1;3]|[7;9;10-13]}Java-如何验证此字符串?,java,string,formatting,Java,String,Formatting,Java中是否存在执行以下此类任务的工具 我得到了这个硬输入字符串:{[1;3]|[7;9;10-13]} 花括号{}表示这是必需的 方括号[]表示所需的组 双管| |的意思是“或” 阅读上面的字符串,我们得到以下结果: 要求某些字符串具有1和3或7和9以及10、11、12和13 如果是真的,它将通过。如果为false,则不会通过 我试图用硬编码来实现这一点,但我觉得有一种更简单或正确的方法来实现这种类型的验证 我必须学习哪种类型的内容才能了解更多信息 我从这段代码开始,但我觉得这不对:
- 花括号{}表示这是必需的
- 方括号[]表示所需的组
- 双管| |的意思是“或”
- 要求某些字符串具有1和3或7和9以及10、11、12和13
//Gets the string
String requiredGroups = "{[1;3]||[7;9;10-13]}";
//Gets the groups that an Object belongs to
//It will return something like 5,7,9,10,11,12
List<Integer> groupsThatAnObjectIs = object.getListOfGroups();
//Validate if the Object is in the required groups
if ( DoTheObjectIsInRequiredGroups( groupsThatAnObjectIs, requiredGroups ) ) {
//Do something
}
//获取字符串
字符串requiredGroups=“{[1;3]|[7;9;10-13]}”;
//获取对象所属的组
//它将返回5,7,9,10,11,12之类的值
List groupsThatAnObjectIs=object.getListOfGroups();
//验证对象是否在所需的组中
if(对象是否在需要的组(组是否为需要的对象,需要的组)){
//做点什么
}
我试图使用这个迭代器从requiredGroups变量中获取所需的值
//Used for values like {[1;3]||[9;10;11-15]} and returns the required values
public static void IterateRequiredValues(String values, List<String> requiredItems) {
values = values.trim();
if( !values.equals("") && values.length() > 0 ) {
values = values.replace("{", "");
values = values.replace("}", "");
String arrayRequiredItems[];
if ( values.contains("||") ) {
arrayRequiredItems = values.split("||");
}
//NOTE: it's not done yet
}
}
//用于{[1;3]|[9;10;11-15]}等值,并返回所需的值
公共静态void iteratererequiredValues(字符串值、列表requiredItems){
values=values.trim();
如果(!values.equals(“”&&values.length()>0){
值=值。替换(“{,”);
值=值。替换(“}”和“”);
字符串数组请求项[];
if(values.contains(“| |”){
ArrayRequestItems=values.split(“| |”);
}
//注意:还没有完成
}
}
所以规则对我来说不是很清楚。
例如,您是只关注|
还是也有&&
?
如果我看一下您的示例,我可以从中得出&&和运算符在中是隐式的代码>
尽管如此,我还是制作了一个代码示例(没有太多正则表达式)来检查您的规则
首先,您需要从|
操作符开始。
将所有不同的OR语句放入字符串块中
接下来,您需要检查字符串
块中的每个元素,并检查输入值是否包含所有块值
如果是这样,那么您的输入字符串必须包含您设置的所有规则
如果规则包含一个范围,则必须首先完全填充范围块
然后对范围块执行与普通规则值相同的操作
完成下面的代码示例
package nl.stackoverflow.www.so;
import java.util.ArrayList;
import java.util.List;
public class App
{
private String rules = "{[1;3] || [7;9;10-13] || [34;32]}";
public static void main( String[] args )
{
new App();
}
public App() {
String[] values = {"11 12", "10 11 12 13", "1 2 3", "1 3", "32 23", "23 32 53 34"};
// Iterate over each value in String array
for (String value : values) {
if (isWithinRules(value)) {
System.out.println("Success: " + value);
}
}
}
private boolean isWithinRules(String inputValue) {
boolean result = false;
// || is a special char, so you need to escape it with \. and since \ is also a special char
// You need to escape the \ with another \ so \\| is valid for one | (pipe)
String[] orRules = rules.split("\\|\\|");
// Iterate over each or rules
for (String orRule : orRules) {
// Remove [] and {} from rules
orRule = orRule.replace("[", "");
orRule = orRule.replace("]", "");
orRule = orRule.replace("{", "");
orRule = orRule.replace("}", "");
orRule.trim();
// Split all and rules of or rule
String[] andRules = orRule.split(";");
boolean andRulesApply = true;
// Iterate over all and rules
for (String andRule : andRules) {
andRule = andRule.trim();
// check if andRule is range
if (andRule.contains("-")) {
String[] andRulesRange = andRule.split("-");
int beginRangeAndRule = Integer.parseInt(andRulesRange[0]);
int endRangeAndRule = Integer.parseInt(andRulesRange[1]);
List<String> andRangeRules = new ArrayList<String>();
// Add all values to another rule array
while (beginRangeAndRule < endRangeAndRule) {
andRangeRules.add(Integer.toString(beginRangeAndRule));
beginRangeAndRule++;
}
for (String andRangeRule : andRangeRules) {
// Check if andRule does not contain in String inputValue
if (!valueContainsRule(inputValue, andRangeRule)) {
andRulesApply = false;
break;
}
}
} else {
// Check if andRule does not contain in String inputValue
if (!valueContainsRule(inputValue, andRule)) {
andRulesApply = false;
break;
}
}
}
// If andRules apply, break and set bool to true because string contains all andRules
if (andRulesApply) {
result = true;
break;
}
}
return result;
}
private boolean valueContainsRule(String val, String rule) {
boolean result = true;
// Check if andRule does not contain in String inputValue
if (!val.contains(rule)) {
result = false;
}
return result;
}
}
package nl.stackoverflow.www.so;
导入java.util.ArrayList;
导入java.util.List;
公共类应用程序
{
私有字符串规则=“{[1;3]| |[7;9;10-13]| |[34;32]}”;
公共静态void main(字符串[]args)
{
新应用程序();
}
公共应用程序(){
字符串[]值={“11 12”、“10 11 12 13”、“1 2 3”、“1 3”、“32 23”、“23 32 53 34”};
//迭代字符串数组中的每个值
for(字符串值:值){
如果(在规则(值)内){
System.out.println(“成功:+值);
}
}
}
私有布尔值isWithinRules(字符串输入值){
布尔结果=假;
//| |是一个特殊字符,因此需要使用\.对其进行转义,因为\也是一个特殊字符
//您需要使用另一个\来转义\以便\\|对一个|(管道)有效
String[]orRules=rules.split(“\\\\\\\\\\”);
//迭代每个或多个规则
用于(字符串或规则:或规则){
//从规则中删除[]和{}
orRule=orRule.replace(“[”,”);
orRule=orRule.replace(“]”,“”);
orRule=orRule.replace(“{,”);
orRule=orRule.replace(“}”和“”);
orRule.trim();
//拆分或规则的所有和规则
字符串[]和规则=orRule.split(“;”);
布尔andRulesApply=true;
//迭代所有规则
for(字符串和规则:和规则){
andRule=andRule.trim();
//检查andRule是否在范围内
if(andRule.contains(“-”){
字符串[]和规则范围=andRule.split(“-”);
int beginrangerandrule=Integer.parseInt(andRulesRange[0]);
int endRangeAndRule=Integer.parseInt(andRulesRange[1]);
List and RangeRules=新建ArrayList();
//将所有值添加到另一个规则数组
while(beginRangeAndRule